WHERE 子句 IN group_concat

发布于 2024-10-05 05:30:10 字数 1548 浏览 6 评论 0原文

我有以下查询:

SELECT name, GROUP_CONCAT(job SEPARATOR ','),
    count(*) FROM users GROUP BY name ORDER BY name ASC

是否可以仅连接不包含 '' 的作业字段,如果可以,如何连接?我无法执行 WHERE 子句,因为我仍然需要那些没有“工作”的子句。

想象一下下表:

+-------+----------+
| name  | job      |
+-------+----------+
| Bob   | doctor   |
| Bob   | teacher  |
| Frank | dentist  |
| Tom   |          |
| Tom   |          |
| Tom   |          |
| Tom   |          |
| Tom   | salesman |
+-------+----------+

当前查询将导致:

+--------+---------------------------------+----------+
| name   | GROUP_CONCAT(job SEPARATOR ',') | count(*) |
+--------+---------------------------------+----------+
| Bob    | doctor, teacher                 | 2        |
| Frank  | dentist                         | 1        |
| Tom    | ,,,,salesman                    | 5        |
+--------+---------------------------------+----------+

但我希望它是:

+--------+---------------------------------+----------+
| name   | GROUP_CONCAT(job SEPARATOR ',') | count(*) |
+--------+---------------------------------+----------+
| Bob    | doctor,teacher                  | 2        |
| Frank  | dentist                         | 1        |
| Tom    | salesman                        | 5        |
+--------+---------------------------------+----------+

(不要介意这个表的逻辑。这只是一个简单的例子来说明问题)

这样做的原因是,我将在php中使用explode() GROUP_CONCAT(job SEPARATOR ',') ,而在实际的表中,很容易就会有1000个无用的,, ,这会让explode函数变得非常慢。 谢谢,

主斯蒂克斯。

I've got the following query:

SELECT name, GROUP_CONCAT(job SEPARATOR ','),
    count(*) FROM users GROUP BY name ORDER BY name ASC

Is it possible to only concat the job fields that do not contain '', and if so how to? I can't do a WHERE clause, because I still need to ones that don't have a 'job'.

Picture the following table:

+-------+----------+
| name  | job      |
+-------+----------+
| Bob   | doctor   |
| Bob   | teacher  |
| Frank | dentist  |
| Tom   |          |
| Tom   |          |
| Tom   |          |
| Tom   |          |
| Tom   | salesman |
+-------+----------+

The current query would result in:

+--------+---------------------------------+----------+
| name   | GROUP_CONCAT(job SEPARATOR ',') | count(*) |
+--------+---------------------------------+----------+
| Bob    | doctor, teacher                 | 2        |
| Frank  | dentist                         | 1        |
| Tom    | ,,,,salesman                    | 5        |
+--------+---------------------------------+----------+

But I want it to be:

+--------+---------------------------------+----------+
| name   | GROUP_CONCAT(job SEPARATOR ',') | count(*) |
+--------+---------------------------------+----------+
| Bob    | doctor,teacher                  | 2        |
| Frank  | dentist                         | 1        |
| Tom    | salesman                        | 5        |
+--------+---------------------------------+----------+

(Don't mind the logic of this table. It is just a simple example to make things clear)

Reason for this is, I am going to explode() the GROUP_CONCAT(job SEPARATOR ',') in php, and in the actual table, there will easily be 1000 useless ,,'s and this would make the explode function very slow.
Thanks,

lordstyx.

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

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

发布评论

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

评论(3

苏璃陌 2024-10-12 05:30:10

我找到了一个解决方案:我不会将作业设置为“”,而是将字段设置为空,并且查询会忽略它。

I've found a solution: instead of making the job '', I'll just make the field Null, and the query ignores it.

那小子欠揍 2024-10-12 05:30:10

您还可以使用IF()

SELECT name, GROUP_CONCAT( if(job = '', null, job) SEPARATOR ','),
    count(*) FROM users GROUP BY name ORDER BY name ASC

You can also use IF():

SELECT name, GROUP_CONCAT( if(job = '', null, job) SEPARATOR ','),
    count(*) FROM users GROUP BY name ORDER BY name ASC
潇烟暮雨 2024-10-12 05:30:10
 SELECT name, GROUP_CONCAT(job SEPARATOR ',') FROM USERS
    WHERE job IS NULL OR job NOT LIKE '%,%'
    GROUP BY name ORDER BY name ASC

但是,最好建议您将逗号替换为其他字符串,然后包含所有作业:

 SELECT name, GROUP_CONCAT(REPLACE(job, ',', ';')) FROM USERS
    GROUP BY name ORDER BY name ASC
 SELECT name, GROUP_CONCAT(job SEPARATOR ',') FROM USERS
    WHERE job IS NULL OR job NOT LIKE '%,%'
    GROUP BY name ORDER BY name ASC

However, you might be better advised to string replace the comma to something else and then include all the jobs:

 SELECT name, GROUP_CONCAT(REPLACE(job, ',', ';')) FROM USERS
    GROUP BY name ORDER BY name ASC
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文