日期间隔查询、分页查询、联接查询汇总

条件部分示例代码如下:

OrderDateTime>='" + dateTimePicker1.Value.Date + "' and OrderDateTime<'" + dateTimePicker2.Value.AddDays(1).Date + "' ";

假设定义开始日期为beginDateTime,结束日期为endDateTime,那么在查询日期间的数据,有时具体到时间时,需要对条件进行如下设置:

where(o=>o.OrderDateTime>=beginDateTime&&o.OrderDateTime.<endDateTime.Value.AddDays(1).Date

 

连接查询

left join(左联接) 返回包括左表中的所有记录和右表中联结字段相等的记录
right join(右联接) 返回包括右表中的所有记录和左表中联结字段相等的记录
inner join(等值连接) 只返回两个表中联结字段相等的行

说到mySQL啊,用了挺久的了,但是有个问题一直在困扰着我,就是left join、join、right join和inner join等等各种join的区别。网上搜,最常见的就是一张图解图,如下:

SQL行转列,列转行

--行转列的静态方案一:CASE WHEN,兼容sql2000
select custid,
sum(case when YEAR(orderdate)=2002 then qty end) as [2002],
sum(case when YEAR(orderdate)=2003 then qty end) as [2003],
sum(case when YEAR(orderdate)=2004 then qty end) as [2004]
from orders
group by custid;
GO
--行转列的静态方案二:PIVOT,sql2005及以后版本
select *
from (select custid,YEAR(orderdate) as years,qty from orders) as ord
pivot(sum(qty) for years in([2002],[2003],[2004]))as p
GO
声明:本站内容来源于原创和互联网,尊重作者版权,转载请注明来源网址,欢迎收藏,谢谢!