sql多表联合查询传入的日期参数对应两个字段,怎么判断一个字段为空则查询另一个字段,反之查询这个字段?求

sql中多表联合查询传入的日期参数对应两个字段,怎么判断A字段为空则查询B字段,如果A字段有数据则查询A这个字段?
select tc.classid,tc.title,td.date,td.changedate,tc.customerid,c.name as customername,p.name as projectname,u.user_id as userid,u.fullname,td.trainerid,td.realtrainerid,tl.name as trainerlevelname,td.starttime,td.changestartime,td.endtime,td.changeendtime,td.role,tl.price,tl.time,tl.discount
,tm.starttime as tmstarttime,tm.endtime as tmendtime,tm.weekday as tmweekday
from t_class_detail td
INNER JOIN t_class_instance tci on td.classinstanceid=tci.classinstanceid
INNER JOIN t_class tc on tci.classid=tc.classid
INNER JOIN t_customer c on tc.customerid=c.customer_id
INNER JOIN t_project p on p.project_id=tc.projectid
INNER JOIN t_trainer t on t.trainerid=td.trainerid
INNER JOIN t_user u on t.userid=u.user_id
INNER JOIN t_trainer_level tl on t.trainerlevelid=tl.trainerlevelid
INNER JOIN t_trainer_time tm on tm.trainerid=t.trainerid
where c.parent_id=87 and tm.weekday like '%3%'
and tc.customerid=112 and t.status=1 and tm.status=1 and CONCAT(td.changedate,td.date)like '%2019-01-13%' ------最后这一句用CONCAT不对,求大佬更正!万分感谢!!!两个字段分别是date和changedate

你这看着那么麻烦,举个例子,自己套进去好了。

select case when 字段A is null then 字段B else 字段A end from 表名 where ……

连接和where条件正常使用。

追问

select case when changedate='' or changedate is null then date else changedate end from t_class_detail where 后面根据日期查询呢,传入日期条件匹配这两个字段查询???

追答

查询时间段正常传入

温馨提示:答案为网友推荐,仅供参考
第1个回答  2019-01-13
select  *,(case when 字段A is null then 字段B else 字段A end ) 新字段名
from 表名 
where ...

concat是MySQL里的语句,SQL里面直接用+号连接就可以了

追问

select case when changedate='' or changedate is null then date else changedate end from t_class_detail where 后面根据日期查询呢,传入日期条件匹配这两个字段查询???

追答

我不了解你具体是要怎么传入日期条件,但如果你不知道在上面条件里怎么加入,那最简单的办法就是把上面所有的内容做为子查询好了
select * from
(select..........) T
where 加入你想要的条件

相关了解……

你可能感兴趣的内容

本站内容来自于网友发表,不代表本站立场,仅表示其个人看法,不对其真实性、正确性、有效性作任何的担保
相关事宜请发邮件给我们
© 非常风气网