【问题标题】:Can't use outer query's column inside inner query不能在内部查询中使用外部查询的列
【发布时间】:2017-03-24 05:53:44
【问题描述】:

如果我在 where 子句中放置一个固定数字,我有这个 mysql 查询似乎可以工作,但是当我尝试用外部查询中的列替换该数字时,我收到以下错误:

错误代码:1054。“where 子句”中的未知列“a.latest_version_id”。

我尝试了各种方法来做这个查询,并重新做了 3 次。请在下面查看我的查询,看看是否有其他人可以看到我做错了什么。我用##### 做笔记。

基本上,我想使用a.latest_version_idb.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


【解决方案1】:

试试这个:

SELECT  
    a.version_id,
    a.title, 
    a.email, 
    a.contact_first_name, 
    a.contact_last_name,
    a.physical_address_1, 
    a.physical_address_2, 
    a.physical_suburb, 
    a.phone_number, 
    a.phone_code, 
    a.latest_version_id,
    a.name,
    f.categories 
FROM

(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

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) as a


    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 

     ) as f ON f.version_id = a.latest_version_id

 WHERE  f.version_id = a.latest_version_id and a.version_id IS NOT NULL AND a.title IS NOT NULL

【讨论】:

  • 嗨@reds,谢谢,但它仍然存在与以前相同的问题 a.latest_version_id
  • 这个“WHERE e.version_id = a.latest_version_id”是你的问题。您正在使用 a.latest_version_id 但未在范围内声明..。更新上面的答案只是修改你的where子句
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-02-19
相关资源
最近更新 更多