-1

在下表中,我想显示在is_logged_in标志中同时显示为 0 和 1 的每个user_id 。

__________________________
| user_id | is_logged_in |
+------------------------+
|    A    |      1       |
+------------------------+
|    B    |      1       |
+------------------------+
|    B    |      0       |
+------------------------+
|    C    |      0       |
+------------------------+
|    D    |      0       |
+------------------------+
|    D    |      1       |
+------------------------+
|    C    |      0       |
+------------------------+

可以在没有 GROUP BY 条件的情况下完成吗?如果可能的话,你能告诉我如何在没有 GROUP BY 的情况下进行查询。谢谢。

查询后的结果:

___________
| user_id |
+---------+
|    B    |
+---------+
|    D    |
+---------+
4

3 回答 3

2

你试图实现的逻辑是什么?为什么要返回 B 和 D 而不是 A 或 C?

如果问题是“向我展示标志user_id中同时显示 0 和 1 的所有内容”is_logged_in

select user_id
  from table_name
 group by user_id
having count( distinct is_logged_in ) = 2

或者

select user_id
  from table_name
 where is_logged_in = 0
intersect
select user_id
  from table_name
 where is_logged_in = 1

或者

select a.user_id
  from table_name a,
       table_name b
 where a.rowid != b.rowid
   and a.user_id = b.user_id
   and a.is_logged_in = 0
   and b.is_logged_in = 1
于 2013-07-02T07:20:28.847 回答
0
SELECT *
FROM table
WHERE (user_id = 'B' or user_id = 'D')
于 2013-07-02T07:17:13.093 回答
0
select distinct user_id
from table_name a 
inner join table_name b on a.user_id = b user_id
where a.is_logged_in <> b.is_logged_in
于 2013-07-02T07:18:55.200 回答