2016-09-13 64 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