自从我执行任何SQL以来已经有一段时间了,我目前正在尝试将一些表左移到旧的JOIN查询中,以便能够获取某些数据。
但是我收到了“未知子句'a.shop_id'i on子句”,我不确定为什么'a.shop_id'是未知的。
SELECT
e.campaign_desc,
e.campaign_type_id,
e.shop_id AS campaign_shop_id,
a.*,
b.*,
a.id AS shop_item_id,
c.cat_id AS category_id
FROM
`shop_item` a,
`shop` b,
`product` c
LEFT JOIN
`campaign_category` d ON
d.category_id = c.cat_id
LEFT JOIN
`campaign` e ON
e.`id` = d.`campaign_id`
AND e.`shop_id` = a.`shop_id`
WHERE
a.`product_id` = 23 AND
b.`id` = a.`shop_id` AND
b.`active_shop` = 1 AND
a.`product_id`= c.`id`
ORDER BY
a.`item_price` DESC,
a.`shop_id`
所以我不得不尝试一些不同的东西,所以我要测试另一个查询想法,并且这个想法很好。但是当然那不是我想要的,因为我想要多个数据列。
SELECT
a.*,
a.shop_id,
b.*,
a.id AS shop_item_id,
c.cat_id AS category_id,
(SELECT t2.campaign_desc
FROM
campaign_category t1, campaign t2
WHERE
t1.category_id = c.cat_id AND
t2.id = t1.campaign_id AND
t2.shop_id = a.`shop_id`
)AS Campaigndata
FROM
`shop_item` a,
`shop` b,
`product` c
WHERE
a.`product_id` = 23 AND
b.`id` = a.`shop_id` AND
b.`active_shop` = 1 AND
a.`product_id`= c.`id`
ORDER BY
a.`item_price` DESC,
a.`shop_id`
我在第一个查询中缺少什么?帮助将不胜感激。
最好的问候
答案 0 :(得分:1)
从不在FROM
子句中使用逗号。 始终使用正确的,明确的标准 JOIN
语法。
因此,将查询改为:
SELECT . . .
FROM shop_item si JOIN
shop s
ON s.id = si.shop_id JOIN
product p
ON si.product_id = p.id LEFT JOIN
campaign_category cc
ON cc.category_id = p.cat_id LEFT JOIN
campaign c
ON c.id = cc.campaign_id
c.shop_id = si.shop_id
WHERE si.product_id = 23 AND
s.active_shop = 1
. . .
请注意,我还更改了表别名,因此它们是表名的缩写,而不是无意义的随机字母。
查询的问题是,
影响使用的标识符的范围。理解这一点实际上并不重要,因为更重要的一点是始终使用正确,明确,标准 JOIN
语法。