简体   繁体   English

在GROUP_CONCAT中搜索

[英]Search within GROUP_CONCAT

I am executing this SQL from a big result of rows 我正在从行的大结果执行此SQL

SELECT userid, group_concat(locationid) FROM user_location group by userid having group_concat(locationid) = 10 SELECT用户ID,GROUP_CONCAT(locationid)FROM user_location组由具有用户ID GROUP_CONCAT(locationid)= 10

userid  locationid
---------  ----------
894801  10,10,10,10,10,10,10,10,10,10,10,10
898356  10,10,11,10
900424  10,10,13,12,12,12,12
902123  10
904910  10,10
907922  10,10,10
912587  10,12,12
930319  10

Now, I want only those locationid rows where the value = 10 and no other value 现在,我只希望其中的locationid行的值= 10而没有其他值

Desired Output: 所需输出:

userid  locationid
---------  ----------
894801  10,10,10,10,10,10,10,10,10,10,10,10
902123  10
904910  10,10
907922  10,10,10
930319  10

I explored and found find_in_set() but no use here 我探索并找到了find_in_set(),但这里没有用

Don't use the result from group_concat() . 不要使用group_concat()的结果。 Just use a simple having clause: 只需使用一个简单的having子句:

SELECT userid, group_concat(locationid)
FROM user_location
GROUP BY userid 
HAVING SUM(locationid = 10) = COUNT(*)

The SUM() counts the number of values that are equal to 10 . SUM()计算等于10的值的数量。 The = COUNT(*) simply says that all are 10. = COUNT(*)仅表示全部为10。

You can also do this by being sure that no values are not 10: 您还可以通过确保所有值都不为10来做到这一点:

HAVING SUM(locationid <> 10) = 0
SELECT * FROM user_location WHERE userid not in (select userid from user_location where locationid<>10) 
group by userid having locationid=10;

Try this.. It's working 试试这个。。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM