具有附加条件的 MySQL GROUP BY

发布于 2025-01-06 09:14:47 字数 525 浏览 0 评论 0原文

我目前正在使用此 MySQL 语句:

SELECT * FROM jobs GROUP BY jobType, address1;

我需要能够向此查询添加另一个条件。 GROUP BY 仅合并重复项,但我还需要合并 jobType 为“1”或 jobType 为“2”的所有行。所有其他 jobTypes 将正常分组。

例如,我的表看起来像这样:

jobType | address1
1       | 123 State st
2       | 123 State st
3       | 415 5th Ave
1       | 123 State st
1       | 24 3rd Ave
1       | 123 State st
3       | 555 Mission st
4       | 123 State st

我想合并第 1 行和第 2 行,即使它们的 jobType 不同。同样,任何 jobType 不是 1 或 2 的行都会被正常分组。

I'm currently working with this MySQL statement:

SELECT * FROM jobs GROUP BY jobType, address1;

I need to be able to add another condition to this query. GROUP BY only combines duplicates, but I also need to combine all rows that have a jobType of '1' or a jobType of '2'. All other jobTypes will be grouped normally.

For example, my table looks something like this:

jobType | address1
1       | 123 State st
2       | 123 State st
3       | 415 5th Ave
1       | 123 State st
1       | 24 3rd Ave
1       | 123 State st
3       | 555 Mission st
4       | 123 State st

I want to combine rows 1 and 2, even though their jobType is different. Again, any rows with a jobType other than either 1 or 2 would be grouped normally.

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

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

发布评论

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

评论(1

瑕疵 2025-01-13 09:14:47
SELECT * 
FROM jobs 
GROUP BY CASE WHEN jobType in ('1','2') THEN '1' ELSE jobType END, address1
SELECT * 
FROM jobs 
GROUP BY CASE WHEN jobType in ('1','2') THEN '1' ELSE jobType END, address1
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文