2015-04-05 75 views
-1

用戶ID的收益名單上有表「battle_user」管理戰役和用戶表MySQL的:與條件

表「battle_user」結構之間的關係:

battle_id  user_id 
1111   1 
1111   2 
22    1 
22    3 
3    2 
3    3 

如何reutrn「USER_ID」名單與特定的用戶, 紛紛上陣,如用戶ID的退貨單有USER_ID戰鬥= 1

的結果應該是:

user_id 
2 
3 

回答

0

我寫了一個查詢:

select distinct(b.user_id) 
from battle_user b 
inner join battle_user b2 on b.battle_id = b2.battle_id and b2.user_id = 10 
where b.user_id <> 10 
0

你可以做

select 
b1.user_id from battle_user b1 
where b1.user_id<>1 
and exists ( 
    select 1 from battle_user b2 
    where 
    b2.battle_id = b1.battle_id 
    and b2.user_id = 1 
); 
1
SELECT distinct(user_id) FROM battle_user 
WHERE battle_id IN (select battle_id from battle_user where user_id = 1) and user_id <> 1