我的数据库中有两个单独的表,这里是相关字段:
表格图片:
CREATE TABLE `images` (
`image_id` int(4) NOT NULL AUTO_INCREMENT,
`project_id` int(4) NOT NULL,
`user_id` int(4) NOT NULL,
`image_name` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
`image_description` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
`date_created` date NOT NULL,
`link_to_file` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
`link_to_thumbnail` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
`given_name` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
`note` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
PRIMARY KEY (`image_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci AUTO_INCREMENT=51 ;
和表项目:
CREATE TABLE `projects` (
`project_id` int(4) NOT NULL AUTO_INCREMENT,
`user_id` int(4) NOT NULL,
`project_name` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
`project_description` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
`date_created` date NOT NULL,
`date_last_edited` date NOT NULL,
`shared` int(1) NOT NULL,
`password` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
PRIMARY KEY (`project_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci AUTO_INCREMENT=25 ;
我想在变量 $content 中显示每个项目中最旧图像的画廊,作为指向该项目页面的链接,我不知道应该如何构建 mysql 查询。你能帮我解决这个问题吗?我尝试了几个 if 和 while 语句,但结果完全失败,我的(非常有限的)知识已经到了尽头。我要跳出窗外了……
所以我想结束
<a href="index.php?page=projects&id='.$projectid.'">
<img src="oldest_photo_of_project_x" />
</a>
<a href="index.php?page=projects&id='.$projectid.'">
<img src="oldest_photo_of_project_y" />
</a>
<a href="index.php?page=projects&id='.$projectid.'">
<img src="oldest_photo_of_project_z" />
</a>
更新1:
为了澄清我正在尝试结合:
"SELECT * FROM projects WHERE user_id='$UserID' ORDER BY project_id DESC"
也许是这样的:
$query = "SELECT images.project_id, projects.project_name ".
"FROM images, projects ".
"WHERE images.project_id = projects.project_id";