我想选择每个类别,并在此类别中的每个子类别中对每个产品进行计数。
此SQL QUERY返回ID为X
的类别的产品计数,并考虑到其子类别产品。
SELECT
count(*)
FROM productos
INNER JOIN categorias
ON categorias.id = productos.categoria_id
WHERE productos.categoria_id IN (select id
FROM (SELECT * FROM categorias
ORDER BY parent, id) products_sorted,
(SELECT @pv := X) initialisation
WHERE find_in_set(parent, @pv)
AND length(@pv := concat(@pv, ',', id))
OR id = @pv)
AND productos.active IS TRUE AND categorias.active IS TRUE
ORDER BY categorias.pos) as productos
现在,我试图获取我拥有的产品类别的每个类别,这就是我的SQL,但是它在字段列表中显示了未知表cats
。我也尝试没有别名。我想我不能在第二选择中使用它。那么,我该怎么办呢?
SELECT
cats.*,
(SELECT
count(*)
FROM productos
INNER JOIN categorias
ON categorias.id = productos.categoria_id
WHERE productos.categoria_id IN (select id
FROM (SELECT * FROM categorias
ORDER BY parent, id) products_sorted,
(SELECT @pv := cats.id) initialisation
WHERE find_in_set(parent, @pv)
AND length(@pv := concat(@pv, ',', id))
OR id = @pv)
AND productos.active IS TRUE AND categorias.active IS TRUE
ORDER BY categorias.pos) as productos
FROM categorias AS cats ORDER BY cats.parent, cats.pos
表格
+----------------------+
| Catrgorias |
+----------------------+
| id (int 11) |
+----------------------+
| nombre (varchar 255) |
+----------------------+
| parent (int 11) |
+----------------------+
| active (tinyint 11) |
+----------------------+
| pos (int 11) |
+----------------------+
+----+------------+--------+
| id | nombre | parent |
+----+------------+--------+
| 1 | Cat1 | NULL |
+----+------------+--------+
| 2 | Cat2. | NULL |
+----+------------+--------+
| 3 | SubCat1 | 1 |
+----+------------+--------+
| 4 | SubCat2 | 2 |
+----+------------+--------+
| 5 | SubSubCat1 | 3 |
+----+------------+--------+
+-----------------------+
| Productos |
+-----------------------+
| id (int 11) |
+-----------------------+
| name (varchar 255) |
+-----------------------+
| description (text) |
+-----------------------+
| image (varchar 255) |
+-----------------------+
| price (decimal 11,2) |
+-----------------------+
| categoria_id (int 11) |
+-----------------------+
| pos (int 11) |
+-----------------------+
| active (tinyint 11) |
+-----------------------+
答案 0 :(得分:0)
在错误消息中,是否有被感染的线路?我认为问题是
(SELECT @pv := cats.id)
我会更改
(SELECT @pv := id FROM CATEGORIAS)
我不知道这是否适合您,但您的查询中看不到其他结构性问题。
要获得相同的结果,我要编写类似的内容,请检查其是否有效(您确定在注释中写的数字吗?)
SELECT ID, SUM(N) FROM
#COLL DIRETTI
((SELECT A.ID AS ID, COUNT(*) AS N
FROM CATEGORIAS A
LEFT JOIN PRODUCTOS B
ON A.ID = B.CATEGORIA_ID
WHERE A.ACTIVE IS TRUE AND B.ACTIVE IS TRUE
GROUP BY A.ID)
UNION ALL
#PRIMA GENERAZIONE
(SELECT C.PARENT AS ID, D.N AS N
FROM CATEGORIAS C
LEFT JOIN (SELECT A.ID, COUNT(*) AS N
FROM CATEGORIAS A
LEFT JOIN PRODUCTOS B
ON A.ID = B.CATEGORIA_ID
WHERE A.ACTIVE IS TRUE AND B.ACTIVE IS TRUE
GROUP BY A.ID) D
ON C.ID = D.ID
WHERE C.PARENT IS NOT NULL
AND D.N IS NOT NULL)
UNION ALL
#SECONDA GENERAZIONE
(SELECT CC.PARENT AS ID, DD.N AS N
FROM
(SELECT AA.ID AS ID, BB.PARENT AS PARENT
FROM CATEGORIAS AA
LEFT JOIN CATEGORIAS BB
ON AA.PARENT=BB.ID
WHERE BB.PARENT IS NOT NULL) CC
LEFT JOIN (SELECT A.ID, COUNT(*) AS N
FROM CATEGORIAS A
LEFT JOIN PRODUCTOS B
ON A.ID = B.CATEGORIA_ID
WHERE A.ACTIVE IS TRUE AND B.ACTIVE IS TRUE
GROUP BY A.ID) DD
ON CC.ID = DD.ID
WHERE DD.N IS NOT NULL)
UNION ALL
#TERZA GENERAZIONE
(SELECT CCC.PARENT AS ID, DDD.N AS N
FROM
(SELECT AA.ID AS ID, CC.PARENT AS PARENT
FROM CATEGORIAS AA
LEFT JOIN CATEGORIAS BB
ON AA.PARENT=BB.ID
LEFT JOIN CATEGORIAS CC
ON BB.PARENT=CC.ID
WHERE CC.PARENT IS NOT NULL) CCC
LEFT JOIN (SELECT A.ID, COUNT(*) AS N
FROM CATEGORIAS A
LEFT JOIN PRODUCTOS B
ON A.ID = B.CATEGORIA_ID
WHERE A.ACTIVE IS TRUE AND B.ACTIVE IS TRUE
GROUP BY A.ID) DDD
ON CCC.ID = DDD.ID
AND DDD.N IS NOT NULL)) TOT
GROUP BY ID