2013-06-07 21 views
18

İkincisinde bir eşleşme olmasa bile iki tabloya katılmak istiyorum.sağdaki ikinci tabloda satır olmasa bile sağa katıl

tablo kullanıcı:

uid | name 
1  dude1 
2  dude2 

masa hesabı:

uid | accountid | name 
1  1   account1 

tablo istediğim:

uid | username | accountname 
1  dude1  account1 
2  dude2  NULL 

sorgu i ile çalışıyorum:

SELECT user.uid as uid, user.name as username, account.name as accountname 
FROM user RIGHT JOIN account ON user.uid=accout.uid 

ne ben alıyorum: yerine

uid | username | accountname 
1  dude1  account1 
+9

SOL JOIN kullanın. – MicSim

+0

zaten denedi .. – coiso

+6

Lol ... "sol birleştirme" cevapları çığ. –

cevap

22

kullanımını Left Join

SELECT user.uid as uid, user.name as username, account.name as accountname 
FROM user LEFT JOIN account ON user.uid=accountid.uid 
4

deneyin bir LEFT JOIN sorgu ile

SELECT user.uid as uid, user.name as username, account.name as accountname 
FROM user 
LEFT JOIN account 
ON user.uid=accout.uid 

Sana bu görsel sunum de bakabilirsiniz istiyorum JOIN query

4

Sağ tutmak katılmak 2. tablodaki tüm sonuçlar (tüm sonuçları sağdaki tabloda tutar), bir sol birleştirme yapmak veya birleşim tümcesinde kullanıcı ve hesap konumlarını değiştirmek.

Bunu yapması gerektiğine inanıyorum.

+1

'user', mysql http://dev.mysql.com/doc/refman/5.0/en/reserved-words.html içinde bir anahtar kelime değil – Fabio

+0

Evet, sadece baktım, düzeltme için teşekkürler. –

İlgili konular