2016-05-15 32 views
0

好吧,所以我有一個包含optionID,optionName的表。它被稱爲user_options。我有第二個表,其中包含用戶ID,optionID。它被稱爲user_selected_options。Mysql select rows與另一個表的記錄

user_options中的選項在user_selected_options中爲用戶存儲,這很好。現在,在編輯用戶時,我只想顯示user_options中沒有爲user_selected_options中的該用戶選擇的選項作爲選擇框。我怎樣才能做到這一點?

我希望這個問題有道理。請讓我知道,如果不清楚,我會進一步解釋。

感謝您的任何提示。

回答

1

下面是使用not exists一種方法:

select uo.* 
from user_options uo 
where not exists (select 1 
        from user_selected_options uso 
        where uso.optionId = uo.option_id and uso.userId = $userId 
       ); 

爲了獲得最佳性能,在user_selected_options(option_id, userId)創建索引。

+0

真棒,它的伎倆。 – Hashmi

相關問題