2014-04-16 119 views
2

我有一個表是這樣的:加入PSQL三個表(SQL查詢)

UsersDevices: user_id, device_id 
registration_posusercompany: company_id, user_id 
registation_companyprofile: company_id, company_name 

我知道UsersDevicesdevice_id,我現在想知道該設備所屬的COMPANY_NAME。對於該僞代碼是這樣的:

--> Get user_id from UsersDevices where device_id = (I know this device_id) 
--> Get company_id with that user_id from registration_posusercompany 
--> Get company_name with that company_id 

我寫的子查詢做到這一點,但確實子查詢很慢比較JOIN。我如何使用JOIN實現這一點。謝謝

+0

我認爲'psql'你的意思是PostgreSQL? –

+0

是的,先生。我的意思是PostgreSQL。 – pynovice

回答

0
select rc.company_name 
from UsersDevices as u 
    join registration_posusercompany as rp on rp.user_id = u.user_id 
    join registation_companyprofile as rc on rc.company_id = rp.company_id 
where u.device_id = yourvalue 
+0

我在哪裏申請'where device_id = something'? – pynovice

+0

檢查更新的答案 – SagarPPanchal

+0

此外,如果您只想從UsersDevices表中選擇列,那麼您可以執行「從用戶設備中選擇您*作爲...」 –