我有4张桌子:
ad
image
placement
ad_placement - many to many intermediary table between ad and placement
图片有广告FK
结构是这样的:
ad
id | url
1 http://
2 http://
3 http://
placement
id | name
1 name_1
1 name_2
ad_placement
id | ad_id | PLACEMENT_ID 1 1 1 2 1 2 3 2 2
image
id | image | ad_id
1 http:// 1
2 http:// 2
3 http:// 3
我想获取广告中的字段,网址和图片中的图片,其中placement = name_1
' **'在查询中只显示我所指的
SELECT A.id, A.url, I.image as image
FROM ad AS A LEFT JOIN (
SELECT ADP.ad_id FROM ad_placement *AS ADP*
JOIN placement AS P ON ADP.placement_id = P.id **WHERE P.name='name_1'**) AS AP ON A.id=AP.ad_id
LEFT JOIN ( SELECT ad_id,image FROM image) AS I ON I.ad_id = A.id
WHERE A.is_active=TRUE
当我尝试在此行使用where
时出现错误
JOIN ads_adplacement AS P ON ADP.adplacement_id = P.id WHE...
missing FROM-clause entry for table "adp"
删除'哪里'有效,但不是我需要的
我按建议添加了ADP(用斜体看**)我得到相同的结果,比如忽略where子句
答案 0 :(得分:1)
第一次左加入有什么意义?
SELECT A.id, A.url, I.image as image
FROM ad AS A
LEFT JOIN image AS I ON I.ad_id = A.id
WHERE A.is_active=TRUE
编辑:可能你的意思是这个。
SELECT A.id, A.url, I.image as image
FROM ad AS A
LEFT JOIN image AS I ON I.ad_id = A.id
WHERE A.is_active=TRUE and
a.id in
(select ad_id
from ad_placement adp
inner join placement p on p.Id = adp.placement_id
where p.Name='name 1');
答案 1 :(得分:0)
请您分享完整的查询,我想看看您在哪里添加where子句。但是,似乎您要添加两个连接之间的位置。 加入所有表后可以添加的位置。
修改
SELECT A.id, A.url, I.image as image
FROM ad AS A
LEFT JOIN ad_placement AS AP ON A.id=AP.ad_id
LEFT JOIN placement AS P ON AP.placement_id = P.id
LEFT JOIN image I ON I.ad_id = A.id
WHERE A.is_active=TRUE and P.name='name_1'