2013-07-02 128 views
-1

在下表中,我想顯示每個user_id,該列表在中顯示爲0和1兩者is_logged_in標誌。在Oracle/SQL中如何選擇具有相同列id但非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 | 
+---------+ 
+0

我不明白實際問題,我想你可能會添加更多信息。 –

+0

爲什麼你想在沒有'group by'條件下做?這是作業要求的一部分嗎? –

回答

2

什麼是你想實現什麼邏輯?爲什麼你想返回B和D,但不是A或C?

如果問題是 「給我的每user_id與顯示了兩個0,並在is_logged_in標誌1」

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 
0
SELECT * 
FROM table 
WHERE (user_id = 'B' or user_id = 'D') 
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 
相關問題