一对多关系只需要许多人中的一个条目

时间:2017-11-17 08:10:24

标签: mysql sql

首先,感谢您抽出宝贵时间阅读本文

问题:

示例

SELECT Article.ID, Article.Name, Article.Price, EV.EV, Pic.Upload_Datum 
FROM Article
LEFT JOIN Pic ON Articel.ID = Pic.Artikle_ID 
LEFT JOIN Stock ON Stock.ID = Article.Lager_ID
LEFT JOIN EV ON EV.ID = Article.EV_ID 
WHERE Article.Activ = "1" 
AND Stock.trader_ID = "2" 
AND Article.stock_ID = "1"
AND pic.Uploaddate IS NOT BETWEEN 1510903702 AND 1503127702
LIMIT 10

示例resutl

ID上传

2624919 1489736525

2624919 1489736528

2624920 1507295386

2624920 1507295389

2624920 1507295397

这是我得到的结果之一,但我只需要这两个:

2624919 1489736525

2624920 1507295386

现在我得到foreach Article.ID不止一条记录,因为在" pic"表中有任何一篇文章的图片。问题是,我只需要sql找到的第一张图片。

我希望以正确的方式提问。

问候 B3rgi

1 个答案:

答案 0 :(得分:1)

如果您真的不关心要为您的照片拍摄哪些数据,请参阅下面的示例,它将拍摄您文章的第一张照片。

SELECT Article.ID, Article.Name, Article.Price, EV.EV, 

         SELECT  Pic.Upload_Datum 
         FROM    Pic 
         WHERE   Articel.ID = Pic.Artikle_ID 
         LIMIT 1) AS Upload_Datum 
FROM Article
LEFT JOIN Stock ON Stock.ID = Article.Lager_ID
LEFT JOIN EV ON EV.ID = Article.EV_ID 
WHERE Article.Activ = "1" 
  AND Stock.trader_ID = "2" 
  AND Article.stock_ID = "1"
LIMIT 10