-1

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

表格图片:

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

3 回答 3

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 个查询结果的结果。

于 2012-05-26T08:53:41.047 回答
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;
于 2012-05-26T09:00:28.430 回答
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>
";
于 2012-05-26T09:02:52.273 回答