![](/img/trans.png)
[英]SELECT sql with four different tables with primary key and foreign key
[英]SQL SELECT different foreign Key
我需要在兩個表中選擇外鍵彼此匹配,但是我不想重復相同的外鍵。
我在數據庫上有這些表:
|Photo| (0/*) --------- (1)|Advertisement| |Photo| -photo_id -url -advertisement_id |Advertisement| -advertisement_id ...
數據庫數據
|Photo| photo_id | url | advertisement_id 1 | x | 1 2 | x | 1 3 | x | 2 4 | x | 3
我要這個:
ID | URL 1 | x 2 | x -> It is anyone url 3 | x
我在下面嘗試SQL,但是此重復ID =(
SELECT a.advertisement_id as ID, p.url as URL FROM Advertisement a RIGHT JOIN Photo p ON a.advertisement_id = p.advertisement_id LIMIT 6
結果:
ID | URL 1 | http://i0.statig.com.br/bancodeimagens/18/46/2h/18... 2 | http://www.yaves.es/images/Animales/Tiernos-Gatito... 2 | http://images.forwallpaper.com/files/thumbs/previe... 10 | http://www.petmag.com.br/img/gatos/racas/7892/euro... 7 | http://www.gmstatic.com/content/images/1369932859_... 8 | http://www.navegandonaweb.com/wp-content/uploads/2...
有人幫我構造SQL查詢嗎?
SELECT a.advertisement_id as ID, p.url as URL
FROM Advertisement a
RIGHT JOIN Photo p
ON a.advertisement_id = p.advertisement_id
GROUP BY a.advertisement_id
ORDER BY p.photo_id ASC (or DESC ? )
LIMIT 6
要么
SELECT a.advertisement_id as ID, p.url as URL
FROM Advertisement a,Photo p
WHERE a.advertisement_id = p.advertisement_id
GROUP BY a.advertisement_id
ORDER BY p.photo_id ASC (or DESC ? )
LIMIT 6
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.