结合MySQL查询

时间:2012-05-26 08:40:39

标签: php mysql join

我的数据库中有两个单独的表,这里是相关字段:

表格图片:

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>

UPDATE1:

澄清我想要合并:

"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";

3 个答案:

答案 0 :(得分:1)

这样的东西将从数据库中提取图像上的数据。

SELECT * FROM `images` WHERE `project_id`="x" ORDER BY `date_created` ASC LIMIT 1,1;

这将为ID为'x'的项目提取最旧的图像。您可以按如下方式将它们链接起来:

SELECT * FROM `images` WHERE `project_id`="x" ORDER BY `date_created` ASC LIMIT 1,1;
SELECT * FROM `images` WHERE `project_id`="y" ORDER BY `date_created` ASC LIMIT 1,1;
SELECT * FROM `images` WHERE `project_id`="z" ORDER BY `date_created` ASC LIMIT 1,1;

如果您使用的是PHP,则可以使用mysqli_result :: fetch_array来获取应包含所有3个查询结果的结果。

答案 1 :(得分:1)

没有测试错误,但我会做这样的事情:

$result = mysql_query("SELECT DISTINCT 
    `projects`.`project_id` AS `project`, 
    `images`.`link_to_file` AS `filepath`
FROM 
    `projects`,
    `images`
WHERE 
    `projects`.`project_id` = `images`.`project_id`
ORDER BY 
    `images`.`date_created` DESC");

while ($resultLoop = mysql_fetch_array($result)) {
    $str .= '<a href="index.php?page=projects&id=' . $resultLoop["project"] . '">
        <img src="' . $resultLoop["filepath"] . '" />
    </a>';
}


echo $str;

答案 2 :(得分:0)

$dbh = new PDO($DSN, $USERNAME, $PASSWORD);
$qry = $dbh->prepare('
  SELECT   project_id, link_to_thumbnail
  FROM     images NATURAL JOIN (
    SELECT   project_id, MIN(date_created) AS date_created
    FROM     images
    GROUP BY project_id
    WHERE    user_id = ?
  ) AS t
  ORDER BY project_id DESC
');
$qry->bindValue(1, $UserID);
$qry->execute();

while ($row = $qry->fetch()) echo "
  <a href=\"index.php?page=projects&id=$row[project_id]\">
    <img src=\"$row[link_to_thumbnail]\"/>
  </a>
";