gpt4 book ai didi

PHP MYSQL 查询组合并添加两个相等的结果

转载 作者:行者123 更新时间:2023-11-29 04:48:40 25 4
gpt4 key购买 nike

有人请帮我解决我已经花了两天时间的 sql 查询....

我有下面给出的 MYSQL 查询

(SELECT
c.cl_list as cl_list,
c.name as name,
pc.value as value,
count( pc.value) as total
FROM
projs p
LEFT JOIN classify_proj_new pc
ON p.proj_id = pc.proj_id_fk
LEFT JOIN classify_list c
ON c.cl_list = pc.class_id_fk
WHERE
MATCH ( p.title ) AGAINST ( 'jerm' IN BOOLEAN MODE )
GROUP BY
c.cl_list,
pc.value)
UNION ALL
(SELECT
c.cl_list as cl_list,
c.name as name,
pc.value as value,
count( pc.value) as total
FROM
jerm p
LEFT JOIN classify_jerm_new pc
ON p.jerm_id = pc.jerm_id_fk
LEFT JOIN classify_list c
ON c.cl_list = pc.class_id_fk
WHERE
MATCH ( p.jermname ) AGAINST ( 'jerm' IN BOOLEAN MODE )
GROUP BY
c.cl_list,
pc.value)

给出的结果(如下):

  cl_list      name              value        total
------------------------------------------------------------------------------------
1 department jewller 2
3 price 50 2
6 color blue 1
6 color Red 2
1 department jewller 1
6 color Red 1

但我正在尝试获得一个结果,该结果可以添加重复值的总数并避免重复值....类似这样的事情(如下):

  cl_list      name              value        total
------------------------------------------------------------------------------------
1 department jewller 3
3 price 50 2
6 color blue 1
6 color Red 3

有人请帮助我,我对我的输出感到非常难过......

非常感谢您...

最佳答案

从您的查询中选择并按 cl_list、名称和值分组:

SELECT
cl_list,
name,
value,
sum(total) as total
FROM (
-- your current query here ...
) data
GROUP BY
cl_list,
name,
value

关于PHP MYSQL 查询组合并添加两个相等的结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14580073/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com