未知列但该列使用两个内部联接

时间:2018-04-16 08:35:48

标签: mysql join

我有这个SQL:

select * 
from products_to_mz
join mz_categories on products_to_mz.product_type = mz_categories.id
join products_to_mz as ptm on products.products_id = ptm.product_id
order by products_to_mz.product_type

我正试图从数据库中得到这样的东西:

product_id, product_type_id, product_type_name

(这些列有不同的名称,但我想让你知道这是什么意思。)

问题是当products.products_id确实存在时,我得到了这个SQL错误。

  

SQL错误:#1054 - Unknown column 'products.products_id' in 'on clause'

以下是第二个连接表的结构:

products_to_mz结构:

产品结构:

enter image description here

1 个答案:

答案 0 :(得分:0)

在您的查询中产品别名未在任何地方定义,假设它适用于products_to_mz,则查询应为

SELECT *
FROM   products_to_mz products
       JOIN mz_categories ON  products_to_mz.product_type = mz_categories.id
       JOIN products_to_mz AS ptm ON  products.products_id = ptm.product_id
ORDER BY
       products.product_type

 SELECT *
    FROM   products_to_mz 
           JOIN mz_categories ON  products_to_mz.product_type = mz_categories.id
           JOIN products ON  products.products_id = ptm.product_id
    ORDER BY
           products_to_mz .product_type