WHERE 子句 IN group_concat
我有以下查询:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我找到了一个解决方案:我不会将作业设置为“”,而是将字段设置为空,并且查询会忽略它。
I've found a solution: instead of making the job '', I'll just make the field Null, and the query ignores it.
您还可以使用
IF()
:You can also use
IF()
:但是,最好建议您将逗号替换为其他字符串,然后包含所有作业:
However, you might be better advised to string replace the comma to something else and then include all the jobs: