结合MySQL查询

I have two seperate tables in my DB, here the relevant fields:

table images:

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 ;

and table projects:

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 ;

I would like to display in a variable $content, a gallery of the oldest image from each project as a link to that project page and I have no idea how the mysql query should be built. Can you please help me with this? I have tried several if and while statements but the results have been complete failures and i am at the end of my (very limited) knowledge. I'm about to jump out the window...

So I would like to end up with

<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:

To clarify I am trying to combine:

"SELECT * FROM projects WHERE user_id='$UserID' ORDER BY project_id DESC"

And maybe something like this:

$query = "SELECT images.project_id, projects.project_name ". 
"FROM images, projects ".
"WHERE images.project_id = projects.project_id";

Haven't tested for errors, but I'd do something like this:

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

Something like this will extract the data on the images from the database.

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

That will extract the oldest image for the project with ID 'x'. You can chain them up as follows:

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;

If you are using PHP, you can use mysqli_result::fetch_array to get the results which should contain results for all 3 queries.

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