2011-10-04 99 views
0
Users    UserGroup 

id name  rowid GrpID UserID name 
1 aaa   1  1  1  aaa 
2 bbb   2  1  3  ccc 
3 ccc   3  2  1  aaa 
4 ddd   4  2  4  ddd 
5 eee   5  2  5  eee 

我有2個表,用戶和用戶組,我想顯示用戶表中的所有名稱,但除usergroup組中與GrpID關聯的用戶標識外。根據條件選擇名稱的Mysql查詢

for grpID 1 i want to display 
bbb 
ddd 
eee 

我不想顯示用戶ID 2和3,因爲它在GRPID 1.我將有很多GRPID。如何在我的SQL中做到這一點。我不想爲其他羣ID顯示已在爲GRPID 1用戶組表的名稱..和相同條件

回答

1

怎麼樣?

select name from users u 
where u.id not in (
    select userid from usergroup ug 
    where ug.grpid = 1) 
0
Select * from users where userid <> '1' 

由於<>的意思是:是不是

+0

我有很多的groupid我不能做到這一點 – John

+0

我改變GRPID到用戶id,作爲真正引用不是在SQL的基礎上,你的榜樣 –

0

加入您的SQL表。

ANSI連接(像這樣):

select 
    user.id, 
    user.name 
from 
    user 
    inner join usergroup on user.id = usergroup.rowid 
where 
    usergroup.grpid = __parameter goes here__ 

老校友連接(通過WHERE子句):

select 
    user.id, 
    user.name 
from 
    user, 
    usergroup 
where 
    user.id = usergroup.rowid and 
    usergroup.grpid = __parameter goes here__ 
0
select users.name from users, usergroup where users.userid = usergroup.userid and usergroup.grpid <> :grpId