为什么 group_concat 会带来每一行的所有产品 ID

发布于 2024-11-17 10:12:43 字数 1602 浏览 3 评论 0原文

这是我的查询,它有一些连接,除了 GROUP_CONCAT(p.product_id) 和 SUM(p.price) 部分外,所有连接都运行良好。

SELECT ts.name as main_name, tp.class_id, ts.step_number, GROUP_CONCAT(p.product_id) as product_id, SUM(p.price) as price
from template as t 
JOIN template_step as ts on ts.template_id=t.template_id 
JOIN template_product as tp on tp.template_id=ts.template_id 
JOIN product as p on p.product_id=tp.product_id 
JOIN product_description as pd on pd.product_id=p.product_id 
where t.template_id = '59' 
group by tp.class_id, ts.step_number 
ORDER by ts.step_number, tp.class_id

问题是返回的product_ids和sum字段的元素是重复的

这是我的查询数据

Networking  1   1    88,156,151,275,48,101,274,133,154,125,135,148,63,63    3070.0000
Networking  2   1    275,235,164,274,154,124,169,148,62,98,62,277,191,270   3695.0000
Networking  3   1    92,98,216,181,133,187,272,154,274,148,126,62,62,165    4970.0000
Back Office 1   2    63,88,156,151,275,48,101,274,133,154,125,135,148,63    3070.0000
Back Office 2   2    275,235,164,274,154,124,169,148,62,98,62,277,191,270   3695.0000
Back Office 3   2    62,165,92,98,216,181,133,187,272,154,274,148,126,62    4970.0000
Data Back   1   3    148,63,63,88,156,151,275,48,101,274,133,154,125,135    3070.0000
Data Back   2   3    270,275,235,164,274,154,124,169,148,62,98,62,277,191   3695.0000
Data Back   3   3    62,62,165,92,98,216,181,133,187,272,154,274,148,126    4970.0000
Kitchen     1   4    135,148,63,63,88,156,151,275,48,101,274,133,154,125    3070.0000

每个类应该只返回1或2个product_id。如果我可以提供任何其他信息来帮助其他人帮助我,我可以提供任何东西..db 结构..ext..

here is my query which has a few joins and all works well except the GROUP_CONCAT(p.product_id) and the SUM(p.price) portion.

SELECT ts.name as main_name, tp.class_id, ts.step_number, GROUP_CONCAT(p.product_id) as product_id, SUM(p.price) as price
from template as t 
JOIN template_step as ts on ts.template_id=t.template_id 
JOIN template_product as tp on tp.template_id=ts.template_id 
JOIN product as p on p.product_id=tp.product_id 
JOIN product_description as pd on pd.product_id=p.product_id 
where t.template_id = '59' 
group by tp.class_id, ts.step_number 
ORDER by ts.step_number, tp.class_id

The problem is that the elements returned the product_ids and the sum field are repeating

Here is my data from the query

Networking  1   1    88,156,151,275,48,101,274,133,154,125,135,148,63,63    3070.0000
Networking  2   1    275,235,164,274,154,124,169,148,62,98,62,277,191,270   3695.0000
Networking  3   1    92,98,216,181,133,187,272,154,274,148,126,62,62,165    4970.0000
Back Office 1   2    63,88,156,151,275,48,101,274,133,154,125,135,148,63    3070.0000
Back Office 2   2    275,235,164,274,154,124,169,148,62,98,62,277,191,270   3695.0000
Back Office 3   2    62,165,92,98,216,181,133,187,272,154,274,148,126,62    4970.0000
Data Back   1   3    148,63,63,88,156,151,275,48,101,274,133,154,125,135    3070.0000
Data Back   2   3    270,275,235,164,274,154,124,169,148,62,98,62,277,191   3695.0000
Data Back   3   3    62,62,165,92,98,216,181,133,187,272,154,274,148,126    4970.0000
Kitchen     1   4    135,148,63,63,88,156,151,275,48,101,274,133,154,125    3070.0000

There should be only 1 or 2 product_id returned for each class. If there is any other information I can give to help others help me I can provide anything..db structure ..ext..

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

黯淡〆 2024-11-24 10:12:43

GROUP_CONCAT() 不会除非您使用 DISTINCT,否则删除重复的行:

GROUP_CONCAT([DISTINCT] expr [,expr ...]
             [ORDER BY {unsigned_integer | col_name | expr}
                 [ASC | DESC] [,col_name ...]]
             [SEPARATOR str_val])

GROUP_CONCAT() will not remove duplicate rows unless you use DISTINCT:

GROUP_CONCAT([DISTINCT] expr [,expr ...]
             [ORDER BY {unsigned_integer | col_name | expr}
                 [ASC | DESC] [,col_name ...]]
             [SEPARATOR str_val])
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文