常用SQL语句

[b]1、常用的Update语句:[/b]
update a set tier_id = l.tier_id
from temp_id a,lp_account l
where a.aid=l.aid
或者
update a set tier_id = l.tier_id
from a inner join l on a.id = l.tid
[b]2、常用的Delete语句:[/b]delete from t1 from t1 inner join t2 on t1.id = t2.tid
[b]3、常用的case,count查询语句:[/b]
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
[b]4、经典的左链接查询:[/b]
查出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
[b]5、去除重复记录的SQL[/b]
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
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值