博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
SQL常用语句
阅读量:5054 次
发布时间:2019-06-12

本文共 2589 字,大约阅读时间需要 8 分钟。

发现好记性不如烂笔头这句话是真理已经很久了,呵呵,得真理用于实践了。
1、常用的Update语句:
update temp_id set temp_id.tier_id =lp_account.tier_id from temp_id, lp_account   where temp_id.aid=lp_account.aid 或者 update a set tier_id  = l.tier_id from a inner join l on a.id = l.tid
2、常用的Delete语句:
delete from t1 from t1 inner join t2 on t1.id = t2.tid
3、常用的case,count查询语句:
select fr.departure_airport,fr.arrive_airport, count(case when fr.cumulate_cabin in ('F','A') then 1 else NULL end) as FACount, count(case when fr.cumulate_cabin in ('C','D') then 1 else NULL end) CDCount, count(case when fr.cumulate_cabin='W' then 1 else NULL end) WCount from fr_ffp_travel_detail fr where fr.flight_date>='2010-01-01' and fr.flight_date<='2010-12-01' and ((fr.departure_airport='CAN' and fr.arrive_airport='SYD') or (fr.departure_airport='CAN' and fr.arrive_airport='MEL') or (fr.departure_airport='CAN' and fr.arrive_airport='BNE') or (fr.departure_airport='CAN' and fr.arrive_airport='PEK') or (fr.departure_airport='PEK' and fr.arrive_airport='AMS') or (fr.departure_airport='CAN' and fr.arrive_airport='CDG') or (fr.departure_airport='CAN' and fr.arrive_airport='DXB') or (fr.departure_airport='CAN' and fr.arrive_airport='LAX') or (fr.departure_airport='DXB' and fr.arrive_airport='JED')) and fr.cumulate_cabin in ('F','A','C','D','W') and fr.status='IN' and fr.airline_company='CZ' group by fr.departure_airport,fr.arrive_airport
4、经典的左链接查询:
查出a中b不存在bid的记录:
select * from a left join b on a.aid = b.bid where b.name is null
在连接的时候加上条件,即先过滤,再连接:
select branch_code, isnull(r.result,'NO'),count(c.sid) as allCount  from cb_service_order c inner join fr_soc_flight f on c.departure_airport=f.departure_airport and c.flight_date=f.flight_date and c.flight_no=f.flight_no inner  join cb_service_response r on c.sid=r.sid and r.result_type='001' where c.sid is not null and c.flight_date>=:beginDate and c.flight_date<=:endDate and c.departure_airport in ('SHE','PEK','KWL','NNG','DLC','CAN','KWE','HAK','SYX','CGO','WUH','HRB','CSX','CGQ','SWA','PVG','SHA','URC','SZX','ZUH','CKG') and c.type=:type and c.cabin in ('F','A','P','J','C','D','I','W','Z','Y','T','K','H','M','B','E','G','L','N','O','Q','R','S','U','V','X') group by branch_code, isnull(r.result,'NO') order by branch_code
5、去除重复记录的SQL
select * from gczbxx_zhao where viewid in ( select max(viewid) from gczbxx_zhao group by gcmc ) order by gkrq desc ---还是这个可行。 或者: SELECT * FROM TB A INNER JOIN ( SELECT MIN(ID),NAME FROM TB WHERE TYPE = 1 AND CHECK = 0 GROUP BY NAME ) B ON A.ID = B.ID AND A.NAME = B.NAME

转载于:https://www.cnblogs.com/wuyansheng/archive/2012/03/08/2385794.html

你可能感兴趣的文章
女人心里,总存着一些男人不懂的小事。 【转自豆瓣】
查看>>
CSDN可以直接扣扣登录.....如需查看我的博客去CSDN
查看>>
(转)notepad++去重
查看>>
sqlserver convert 日期时间 转换格式化
查看>>
摆脱DOM操作,从TodoMVC看angularJS
查看>>
[usaco2009nov]奶牛的图片
查看>>
FFT理解
查看>>
App弱网测试方式
查看>>
PHP zendstudio framework2配置过程
查看>>
Xor Sum 01字典树 hdu4825
查看>>
数据访问:三大范式
查看>>
ok6410 android driver(10)
查看>>
kubuntu设置
查看>>
Python基础-----random随机模块(验证码)
查看>>
手机端fixed底部跟着窗口动问题
查看>>
树专题(伸展树 / 树链剖分 / 动态树 学习笔记)
查看>>
HTML图像、超链接标签
查看>>
[国嵌攻略][164][USB驱动程序设计]
查看>>
C# 实现Bresenham算法(vs2010)
查看>>
基于iSCSI的SQL Server 2012群集测试(一)--SQL群集安装
查看>>