简体   繁体   English

仅从左关节中选择一个 +mySQL

[英]SELECT just one FROM the LEFT JOINTs +mySQL

Got following Tables in my SQL-Database (simplified):在我的 SQL 数据库中得到以下表格(简化版):

Table Blogs :博客

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

Table PicturesJoin :表格图片加入

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

Table Pictures :图片

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

PictureJoin "joins" the Picture with the Blog-Table. PictureJoin 将图片与博客表“连接起来”。 Now I use following SQL-Command to joine these two Tables (Blog - PictureJoin) / (PictureJoin - Pictures) together.现在我使用以下 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

The result might look like that:结果可能如下所示:

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

He makes a cross-product out of the available Pictures, which is also logical.他从可用的图片中制作了一个交叉产品,这也是合乎逻辑的。 But I want him to just use the first Picture he finds.但我希望他只使用他找到的第一张图片。 In the end it should look like that:最后它应该是这样的:

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

Tried several hours but couldn't get it to work.尝试了几个小时,但无法让它工作。 Please help!请帮忙!

The easiest approach may be to select one IDPicture per IDBlog from PicturesJoin only:最简单的方法可能是仅从 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;
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;

Try above query.试试上面的查询。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM