MySQL 使用 FIND_IN_SET 和 GROUP_CONCAT 时查询加入问题
MySQL query joining issue while using FIND_IN_SET and GROUP_CONCAT
所以我有一个 product/categories/brands 结构,其中产品类别由包含逗号分隔的类别 ID 列表的列标识,即 101,105,108,以及品牌 ID 列。
我正在尝试获取所有产品的列表,将类别 ID 替换为以逗号分隔的类别名称列表以及品牌名称。
我有以下有效的查询:
SELECT
productid AS product_id,
prodname AS name,
prodcode AS code,
proddesc AS description,
prodprice AS price,
GROUP_CONCAT(c.catname)
FROM
products p,
categories c
WHERE
FIND_IN_SET(c.categoryid, p.prodcatids)
GROUP BY p.productid
然而,当我尝试按如下方式离开加入以获取品牌名称时,它中断并表示 p.prodbrandid 列不存在(它确实存在)。
SELECT
productid AS product_id,
prodname AS name,
prodcode AS code,
proddesc AS description,
prodprice AS price,
b.brandname AS brand,
GROUP_CONCAT(c.catname)
FROM
products p,
categories c
LEFT JOIN
brands b ON p.prodbrandid = b.brandid
WHERE
FIND_IN_SET(c.categoryid, p.prodcatids)
GROUP BY p.productid
如能指出我遗漏的内容,将不胜感激!
来自评论中的建议:
SELECT
p.productid AS product_id,
p.prodname AS name,
p.prodcode AS code,
p.proddesc AS description,
p.prodprice AS price,
b.brandname AS brand,
GROUP_CONCAT(c.catname)
FROM
products p
INNER JOIN categories c on FIND_IN_SET(c.categoryid, p.prodcatids) > 0
LEFT JOIN brands b ON p.prodbrandid = b.brandid
GROUP BY p.productid
虽然将数据存储为逗号分隔列表并不理想;这确实应该拆分为一个额外的 table,它将产品和类别(多个产品可以有多个类别)之间的 many:many 关系分解为两个 1:many 关系(一个产品类别 table, 有一个 productid,categoryid 对)
考虑像这样的一次性操作:
CREATE TABLE ProductCategories(ProductId INT, CategoryId INT)
INSERT INTO ProductCategories
SELECT
p.productid, c.categoryid
FROM
products p
INNER JOIN categories c on FIND_IN_SET(c.categoryid, p.prodcatids) > 0
然后继续使用它,并删除类别列
所以我有一个 product/categories/brands 结构,其中产品类别由包含逗号分隔的类别 ID 列表的列标识,即 101,105,108,以及品牌 ID 列。
我正在尝试获取所有产品的列表,将类别 ID 替换为以逗号分隔的类别名称列表以及品牌名称。
我有以下有效的查询:
SELECT
productid AS product_id,
prodname AS name,
prodcode AS code,
proddesc AS description,
prodprice AS price,
GROUP_CONCAT(c.catname)
FROM
products p,
categories c
WHERE
FIND_IN_SET(c.categoryid, p.prodcatids)
GROUP BY p.productid
然而,当我尝试按如下方式离开加入以获取品牌名称时,它中断并表示 p.prodbrandid 列不存在(它确实存在)。
SELECT
productid AS product_id,
prodname AS name,
prodcode AS code,
proddesc AS description,
prodprice AS price,
b.brandname AS brand,
GROUP_CONCAT(c.catname)
FROM
products p,
categories c
LEFT JOIN
brands b ON p.prodbrandid = b.brandid
WHERE
FIND_IN_SET(c.categoryid, p.prodcatids)
GROUP BY p.productid
如能指出我遗漏的内容,将不胜感激!
来自评论中的建议:
SELECT
p.productid AS product_id,
p.prodname AS name,
p.prodcode AS code,
p.proddesc AS description,
p.prodprice AS price,
b.brandname AS brand,
GROUP_CONCAT(c.catname)
FROM
products p
INNER JOIN categories c on FIND_IN_SET(c.categoryid, p.prodcatids) > 0
LEFT JOIN brands b ON p.prodbrandid = b.brandid
GROUP BY p.productid
虽然将数据存储为逗号分隔列表并不理想;这确实应该拆分为一个额外的 table,它将产品和类别(多个产品可以有多个类别)之间的 many:many 关系分解为两个 1:many 关系(一个产品类别 table, 有一个 productid,categoryid 对)
考虑像这样的一次性操作:
CREATE TABLE ProductCategories(ProductId INT, CategoryId INT)
INSERT INTO ProductCategories
SELECT
p.productid, c.categoryid
FROM
products p
INNER JOIN categories c on FIND_IN_SET(c.categoryid, p.prodcatids) > 0
然后继续使用它,并删除类别列