【发布时间】:2017-03-24 05:53:44
【问题描述】:
如果我在 where 子句中放置一个固定数字,我有这个 mysql 查询似乎可以工作,但是当我尝试用外部查询中的列替换该数字时,我收到以下错误:
错误代码:1054。“where 子句”中的未知列“a.latest_version_id”。
我尝试了各种方法来做这个查询,并重新做了 3 次。请在下面查看我的查询,看看是否有其他人可以看到我做错了什么。我用##### 做笔记。
基本上,我想使用a.latest_version_id 或b.version_id(它们都是相同的值)来过滤我的子查询中的结果。我去掉了 where 子句,但这只是没有结果。
SELECT
DISTINCT
b.version_id,
b.title,
b.email,
b.contact_first_name,
b.contact_last_name,
b.physical_address_1,
b.physical_address_2,
b.physical_suburb,
b.phone_number,
b.phone_code,
a.latest_version_id,
c.name,
f.categories
FROM
products a
LEFT JOIN
product_versions b ON a.latest_version_id = b.version_id
LEFT JOIN
product_version_city c ON b.version_id = c.version_id
##### this is the block of code I am struggling with
LEFT JOIN
(SELECT
e.version_id,
GROUP_CONCAT(d.name SEPARATOR ', ') as categories
FROM
category_product_version e
INNER JOIN
(SELECT
id, name
FROM
categories) AS d ON d.id = e.category_id
WHERE
e.version_id = a.latest_version_id ##### the issue is on this line.
##### It works if I put '1' there instead of a.latest_version_id
) as f ON f.version_id = a.latest_version_id
WHERE
b.version_id IS NOT NULL
AND b.title IS NOT NULL
【问题讨论】:
-
您有四张表,一张用于联系人 (c),一张用于联系方式 (b),一张用于类别 (f),一张用于 ??你能解释一下你想做什么吗?
标签: mysql