我正在建立一个电子商务网站,我有一个管理面板来控制网站。
从管理面板激活类别和产品的过滤器时,此sql查询:
SELECT COUNT(DISTINCT products.product_id) AS products,
companies.company AS range_name,
UPPER(SUBSTRING(companies.company, 1, 1)) AS `index`,
companies.company_id AS range_id,
'S' AS field_type
FROM products
LEFT JOIN companies ON companies.company_id = products.company_id
INNER JOIN products_categories ON products_categories.product_id = products.product_id
LEFT JOIN categories ON categories.category_id = products_categories.category_id
LEFT JOIN companies AS companies ON companies.company_id = products.company_id
WHERE products.status IN ('A')
AND products_categories.category_id IN (261)
AND (categories.usergroup_ids = ''
OR FIND_IN_SET(0, categories.usergroup_ids)
OR FIND_IN_SET(1, categories.usergroup_ids))
AND (products.usergroup_ids = ''
OR FIND_IN_SET(0, products.usergroup_ids)
OR FIND_IN_SET(1, products.usergroup_ids))
AND categories.status IN ('A',
'H')
AND products.status IN ('A')
AND (companies.status = 'A')
GROUP BY products.company_id
ORDER BY companies.company
分解并向我显示错误:
不唯一的表/别名:'公司'数据库(错误):1066
任何人都可以帮助在此查询中找到错误吗?
提前致谢:)
答案 0 :(得分:0)
您要两次加入公司表。这很好,但你必须以不同的方式对它们进行别名。
第一个连接不是别名,所以它被称为相同; 公司即可。第二个是别名,但同样名称为公司,这会导致冲突。
尝试使用不同的名称对两者进行别名,例如 companies1 和 companies2 (或者更好,任何其他更有意义的名称),当然也会相应更新您在select,join,where,group和order中的所有表引用。