【发布时间】:2019-02-20 00:08:17
【问题描述】:
自从我执行任何 SQL 以来已经有一段时间了,我目前正在尝试将一些表左连接到我的旧 JOIN 查询中,以便能够获取某些数据。
但是,我收到“Unknown column 'a.shop_id' i on clause”,但我不确定为什么 '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`
我在第一个查询中遗漏了什么?非常感谢您的帮助。
最好的问候
【问题讨论】:
-
混合隐式和显式连接非常糟糕..