2012-04-04 31 views
1

を選択:コンパウンドSQL私は、次の2つのテーブルを持っている

AUTH_USER

  • ID
  • ユーザ名
  • is_active(ブール値) ...

useprofile_userprofile

  • ID
  • のuser_id ...

にはどうすればis_active = 0すべてのauth_userオブジェクトを見つけるだろうし、そのuser_idとはuserprofile_userprofileオブジェクトがありませんか?例えば、このようなエントリ -

AUTH_USER

userprofile_userprofileにはオブジェクトを有していませんここで、user_id = 1

答えて

2
SELECT 
    * 
FROM 
    auth_user 
WHERE 
    auth_user.is_active=0 
    AND NOT EXISTS 
     (
      SELECT 
       NULL 
      FROM 
       userprofile_userprofile 
      WHERE 
       userprofile_userprofile.user_id=auth_user.id 
     ) 
+0

+1ヌルの選択null – joelparkerhenderson

+0

ありがとうございます。あなたはそこにいたいことがあります。 'linq-to-sqlは' SELECT NULL'を使います:P – Arion

2
select * from auth_user au 
where au.is_active = 0 and 
    not exists(select * from userprofile_userprofile uu where uu.user_id = au.user_id) 
3
SELECT * 
FROM auth_user A 
LEFT JOIN userprofile_userprofile B ON A.id=B.user_id 
WHERE A.is_active = false and B.user_id IS NULL 

B.user_idそれはuser_id=1行を見つけることができないことを意味NULLあります。
これは、テーブルuserprofile_userprofileのIDがすべてNULLではないことを前提としています。別にあなたはまた、あなたが参加表を探しているLEFT JOIN

SELECT 
* 
FROM 
auth_user au 
LEFT JOIN useprofile_userprofile uu ON au.id = uu.user_id 
WHERE uu.id IS NULL 
AND au.is_active = 0 
+0

'A.is_active = false' – David542

+0

@ David542はtypoを編集しました – cctan

1

。このチュートリアルを参照:

http://www.tizag.com/mysqlTutorial/mysqljoins.php

あなたの質問に答えるために、あなたはの線に沿って何かを探しています:

「auth_user.username、auth_user.is_active、useprofile_userprofile.user_id WHERE is_active = 0を選択し、 user_id!= 1 "

0

経由でそれを行うことができ、他のソリューションから

0
select au.id,au.username,count(up.id) from auth_user au 
left outer join useprofile_userprofile up 
on au.id=up.user_id 
where is_active = 1 
group by au.id,au.username 
having count(up.id)=0 
関連する問題