查询1:
Select null as name,Checkinout.* from Checkinout where (Checkinout.userid=1000000000000 and (Checkinout.CheckTime >='2008-01-01 00:00:00') and (Checkinout.CheckTime <='2020-01-01 23:59:59')) Order By Checkinout.CheckTime DESC Limit '0','6'
查询2:
Select UserInfo.name,Checkinout.* from UserInfo left join Checkinout on Checkinout.userid=UserInfo.userid where ((Checkinout.CheckTime >='2008-01-01 00:00:00') and (Checkinout.CheckTime <='2020-01-01 23:59:59')) Order By Checkinout.CheckTime DESC Limit '0','6'
将这两次查询的结果合并后按照Checkinout.CheckTime排序
Select null as name,Checkinout.* from Checkinout where (Checkinout.userid=1000000000000 and (Checkinout.CheckTime >='2008-01-01 00:00:00') and (Checkinout.CheckTime <='2020-01-01 23:59:59')) Order By Checkinout.CheckTime DESC Limit '0','6'
UNION
Select UserInfo.name,Checkinout.* from UserInfo left join Checkinout on Checkinout.userid=UserInfo.userid where ((Checkinout.CheckTime >='2008-01-01 00:00:00') and (Checkinout.CheckTime <='2020-01-01 23:59:59')) Order By Checkinout.CheckTime DESC Limit '0','6'
ORDER BY CheckTime;
()查询1 union 查询2) order by...
用union all 链接两个查询表 在用order by 排序
注意 链接的两个表字段名称要一模一样
union之后 用到group by 就可以
select * from (Select null as name,Checkinout.* from Checkinout where (Checkinout.userid=1000000000000 and (Checkinout.CheckTime >='2008-01-01 00:00:00') and (Checkinout.CheckTime <='2020-01-01 23:59:59')) Order By Checkinout.CheckTime DESC Limit '0','6'
UNION
Select UserInfo.name,Checkinout.* from UserInfo left join Checkinout on Checkinout.userid=UserInfo.userid where ((Checkinout.CheckTime >='2008-01-01 00:00:00') and (Checkinout.CheckTime <='2020-01-01 23:59:59')) Order By Checkinout.CheckTime DESC Limit '0','6' )
ORDER BY CheckTime;
sql 查询语句 ,用UNION或者UNION ALL进行查询结果集的合并,2个查询表必须结果列的数量、类型、别名一致 ;
你在合并的时候,如果过滤重复的值用UNION。如果允许重复的值,用 UNION ALL;
再order by就可以了
用union连接两个表,但是后面要有条件,不能直接加
select a.*,b.* from (select * from tbla) as a,(select * from tblb) as b order by a.xxx,b.yyy
说明: select * from tbla 、select * from tblb等,表示某类查询结果
select * from (select * from tbla union select * from tblb ) order by xxx
说明: 先合并结果,然后在排序显示
union on就可以了,保证查询的字段名保持一致,字段数量不同的时候用“”也是可以的