EDITED
我需要执行一个只带给我 user_id 的查询,他的所有手机确认= 2 ,而不仅仅是一两个,必须全部都是
我的表格如下:
id user_id phone confirmed
------- -------- ----------- ---
1 1 3051234561 1
1 1 3051234562 0
1 1 3051234563 2
1 2 3051234564 2
1 2 3051234565 2
1 2 3051234566 2
1 3 3051234567 0
1 3 3051234568 1
1 4 3051234569 1
在这种情况下,必须返回user_id 2
我试图用GROUP HAVING做这件事,但我没有得到我需要的结果。例如:
SELECT
*
FROM
phones
GROUP BY
user_id
HAVING
MAX(confirmed) = 2
答案 0 :(得分:2)
试试这个
SELECT distinct user_id FROM phones GROUP BY user_id HAVING sum(confirmed) = 2 * count(confirmed)
这将为所有未确认电话的user_id提供
答案 1 :(得分:1)
尝试此查询 -
SELECT
user_id
FROM
phones
GROUP BY
user_id
HAVING
COUNT(IF(confirmed = 2, 1, NULL)) = COUNT(*)
输出:
+---------+
| user_id |
+---------+
| 2 |
+---------+
答案 2 :(得分:0)
在WHERE
GROUP BY
条件
答案 3 :(得分:0)
尝试以下内容....
"SELECT * FROM phones WHERE confirmed='0' GROUP BY user_id"
答案 4 :(得分:0)
尝试这个
SELECT *
FROM phones
Where confirmed= 0
GROUP BY user_id
答案 5 :(得分:0)
您可以尝试这样
SELECT user_id FROM phones WHERE confirmed=0 GROUP BY user_id;
答案 6 :(得分:0)
SELECT distinct user_id FROM phones GROUP BY user_id HAVING sum(confirm)= 0
您写道:
SELECT
*
FROM
phones
GROUP BY
user_id
HAVING
MAX(confirmed) = 2
现在你需要所有确认为0,得到它们的总和必须为零,并且你不想要所有属性而是user_id:
SELECT
distinct(user_id)
FROM
phones
GROUP BY
user_id
HAVING
SUM(confirmed) = 0