我有这个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结构:
产品结构:
答案 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