从LEFT JOINTs + mySQL中只选择一个

时间:2017-06-29 09:39:49

标签: mysql left-join

在我的SQL数据库(简化)中有以下表:

博客

+----+----------------------+----------+
| ID |         Date         | TitleGer |
+----+----------------------+----------+
| 1  | 2017-04-28 15:09:46  | Huhu     |
| 2  | 2017-04-28 15:16:18  | Miau     |
| 3  | 2017-04-28 15:17:14  | Kleff    |
+----+----------------------+----------+

PicturesJoin

+-------------+---------+---------------------+
|  IDPicture  | IDBlog  |        Date         |
+-------------+---------+---------------------+
|         86  |      1  | 2017-06-28 17:41:11 |
|         87  |      1  | 2017-06-28 17:41:11 |
+-------------+---------+---------------------+

图片

+------+-------------------------+---------------------+
|  ID  |        Filename         |        Date         |
+------+-------------------------+---------------------+
|  86  | 20170512200326_320.jpg  | 2017-05-12 20:03:26 |
|  87  | 20170512200326_384.jpg  | 2017-05-12 20:03:30 |
+------+-------------------------+---------------------+

PictureJoin将图片与Blog-Table“连接”。现在我使用以下SQL-Command将这两个表(Blog - PictureJoin)/(PictureJoin - Pictures)连接在一起。

SELECT
  Blogs.ID,
  Blogs.Date,
  TitleGer,
  Pictures.Filename
FROM
  Blogs
LEFT JOIN
  PicturesJoin ON PicturesJoin.IDBlog = Blogs.ID
LEFT JOIN
  Pictures ON Pictures.ID = PicturesJoin.IDPicture
ORDER BY
  DATE DESC

结果可能如下:

+------+----------------------+-----------+------------------------+
|  ID  |        Date          | TitleGer  |       Filename         |
+------+----------------------+-----------+------------------------+
|   1  | 2017-06-28 15:09:46  | Huhu      | 20170512200326_320.jpg |
|   1  | 2017-06-28 15:09:46  | Huhu      | 20170512200326_384.jpg |
|   2  | 2017-04-28 15:16:18  | Miau      | NULL                   |
|   3  | 2017-04-28 15:17:14  | Kleff     | NULL                   |
+------+----------------------+-----------+------------------------+

他从可用的图片中制作了一个交叉产品,这也是合乎逻辑的。但我希望他只使用他发现的第一张照片。最后看起来应该是这样的:

+------+----------------------+-----------+------------------------+
|  ID  |        Date          | TitleGer  |       Filename         |
+------+----------------------+-----------+------------------------+
|   1  | 2017-06-28 15:09:46  | Huhu      | 20170512200326_320.jpg |
|   2  | 2017-04-28 15:16:18  | Miau      | NULL                   |
|   3  | 2017-04-28 15:17:14  | Kleff     | NULL                   |
+------+----------------------+-----------+------------------------+

试了好几个小时但却无法上班。请帮忙!

2 个答案:

答案 0 :(得分:1)

最简单的方法可能是仅从PicturesJoin为每个IDBlog选择一个IDPicture:

SELECT
  b.ID,
  b.Date,
  b.TitleGer,
  p.Filename
FROM Blogs b
LEFT JOIN
(
  SELECT 
    IDBlog, 
    MIN(IDPicture) AS IDPicture
  FROM PicturesJoin 
  GROUP BY IDBlog
) pj ON pj.IDBlog = b.ID
LEFT JOIN Pictures p ON p.ID = pj.IDPicture
ORDER BY b.Date DESC;

答案 1 :(得分:0)

SELECT b.ID,b.Date,b.TitleGer,p.Filename
FROM Blogs b
LEFT JOIN
(
    SELECT main_table.*
    FROM PicturesJoin main_table LEFT JOIN PicturesJoin child_table
     ON (main_table.IDBlog= child_table.IDBlog AND main_table.IDPicture> child_table.IDPicture)
    WHERE child_table.id IS NULL
)
OUTER_TABLE ON OUTER_TABLE .IDBlog = b.ID
LEFT JOIN Pictures p ON p.ID = pj.IDPicture
ORDER BY b.Date DESC;

尝试以上查询。

希望这会对你有所帮助。