赞
踩
左连接left join:以左表为主,查出左表所有数据,关联字段值不相等右边数据显示为空
右连接right join:以右表为主,查出右表中所有数据,关联字段值不相等的左表数据显示为空
内连接inner join :两表关联字段相等的数据
union:查出所有数据,相当于full join(MySQL中没有full join)
复杂查询(查询表中指定列某时间段内数据)
方法一:使用left join
1先查询出时间列
select id,dt from `表名` where dt>='2023-03-28 13:58:26.102' and dt<='2023-03-28 13:58:43.069'
2查询其他列
select dt as data,`列名` from `表名` where dt>='日期时间' and dt<='日期时间' --时间命名别名可以指定列查询显示
左连接关联查询显示指定列
select dt,`列名1`,`列名n` from (select id,dt from `表名` where dt>='2023-03-28 13:58:26.102' and dt<='2023-03-28 13:58:43.069')a1
left join (select dt as data,`列名` from `表名` where dt>='日期时间' and dt<='日期时间') a2 on a1.dt=a2.data --可多表
order by id desc --根据id倒序显示
方法二:使用union all
查询前提:每条查询条件的列必须一致,列类型尽量保持一致
1查询列
select dt ,`【列1】`,'' as `【列2】`,'' as `【列n】` from `【表名】` where 【条件】
select dt ,'' as `【列1】`,`【列2】`,'' as `【列n】` from `【表名】` where 【条件】
select dt ,'' as `【列1】`,'' as `【列2】`,`【列n】` from `【表名】` where 【条件】
2将列用union all关联并把查询数据放新表中命别名
select * from (
select dt ,`【列1】`,'' as `【列2】`,'' as `【列n】` from `【表名】` where 【条件】
union all
select dt ,'' as `【列1】`,`【列2】`,'' as `【列n】` from `【表名】` where 【条件】
union all
select dt ,'' as `【列1】`,'' as `【列2】`,`【列n】` from `【表名】` where 【条件】
) test
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。