2016-09-13 56 views
-1

我只想顯示可用的時間,以達到我想要做到這一點查詢右聯接和WHERE條件Mysql的

select * from agenda right join tbl_hora on agenda.hora_id = tbl_hora.id_hor where agenda.hora_id is null and medico_id = 40 and evento_inicio = '2012-05-05'; 

但是,我檢索0行,如果我請從這裏條件medico_id和evento_inicio,它顯示所有的行,我怎樣才能使查詢顯示可用的時間。

P.D.議程表是空的,因爲是ajax調用和預約。

回答

0

從議程右連接tbl_hora上agenda.hora_id = tbl_hora.id_hora

這意味着從議程表中選擇它的hora_id列有在tbl_hora匹配,並從所有行的行tbl_hora表

其中agenda.hora_id爲空

並從加入後的結果中選擇只有它的agenda.hora_id爲空。

這意味着你總是會得到0結果除非有議程的行,它的.hora_id欄爲空,並在tbl_hora一排,它的.id欄爲空太

你需要刪除where agenda.hora_id is null並使用您搜索的條件代替

+0

如果我刪除agenda.hora_id爲null條件,那麼我的結果將爲0,因爲我的議程表爲空。如何才能更好地選擇不在兩個表中的結果。 – Paul

+0

@Paul你能提供一個來自議程表和tbl_hora表格數據的樣本。因爲你所說的「因爲我的議程表是空的」意味着正確的加入而議程在左邊永遠不會帶來結果! –

+0

我用這個查詢來解決,你怎麼看待它? 'select * from tbl_hora where not exist(在agenda.hora_id = tbl_hora.id_hor和medico_id = 40且evento_inicio ='2012-01-07'的議程中選擇hora_id;'' – Paul