sql小技巧

1. 今天在查询数据的过程中遇到了一个在时间里取日期去查询的难点,最终找到方法了,在此记录一番

 

select * from shop_order_product_details_capital_loss where gross_margin <=44 and DATE_FORMAT(dt,'%Y-%m-%d') = '2021-07-22'

 

2.在数据库中批量造数据

可以通过excel的方式,先将已有的数据导出excel格式,然后在excel中去批量增加,保存后导入到数据库中就可以了

3. 排名有相同值

select name,score,rank from (select 'name',score, @rownum:=@rownum +1 as tmp , @incrnum:=  case when @rowtotal = score then @incrnum when @rowtotal := score then @ rownum end as rank  from (select id , 'name', score from 'score_copy' order by score desc) as a ,(select @rownum:=0, @rowtotal := null ,@incrnum:= 0) b) c where rank<=3

4、计算日期之间的差值:

select TIMESTAMPDIFF(DAY, '2022-06-22 06:00:00','2022-06-21 08:00:14');

posted @ 2021-07-23 19:55  Penny悦  阅读(25)  评论(0编辑  收藏  举报