sql联合查询


SQL语句中的join用法
SQL中join的各种用法

1.自然连接(natural join)
自然连接将表中具有相同名称的列自动进行匹配,自然连接不必指定任何同等连接条件也不能认为指定哪些列需要被匹配,自然连接得到的结果表中,两表中名称相同的列只出现一次。
select * from employee natural join department;

2.内连接(inner join):产生的结果是A和B的交集(相同列里面的相同值)
内连接查询能将左表和右表中能关联起来的数据连接后返回,返回的结果就是两个表中所有相匹配的数据。
select * from TableA as A inner join TableB B on A.PK = B.PK;
select * from TableA as A inner join TableB B on A.PK > B.PK;

3.外连接(outer join)
内连接是要显示两张表的内存,而外连接不要求如此,外连接可以依据连接表保留左表,右表或全部表的行为而分为左外连接右外连接和全连接。
select * from TableA as A left(right/full) join TableB as B on A.PA = B.PK;

Full Join:产生的结果是A和B的并集(如果没有相同的值会用null作为值)

Left Join:产生表A的完全集,而B表中匹配的则有值(没有匹配的则以null值取代)

Right Join:产生表B的完全集,而A表中匹配的则有值(没有匹配的则以null值取代)

COALESCE ( expression,value1,value2……,valuen)
用于对空值的处理
COALESCE()函数的第一个参数expression为待检测的表达式,而其后的参数个数不定,函数将会返回包括expression在内的所有参数中的第一个非空表达式。如果expression不为空值则返回expression,否则判断value1是否是空值,如果value1不为空值则返回value1;否则判断value2是否是空值,如果value2不为空值则返回value2;……以此类推,如果所有的表达式都为空值,则返回NULL。

原文:https://blog.csdn.net/Jerry_991/article/details/86528459

sql case 函数与详细说明

下面是一个是用case函数来完成这个功能的例子

case具有两种格式。简单case函数和case搜索函数。
–简单case函数

case sex
when ‘1’ then ‘男’
when ‘2’ then ‘女’
else ‘其他’ end
–case搜索函数
case when sex = ‘1’ then ‘男’
when sex = ‘2’ then ‘女’
else ‘其他’ end
这两种方式,可以实现相同的功能。简单case函数的写法相对比较简洁,但是和case搜索函数相比,功能方面会有些限制,比如写判断式。
还有一个需要注意的问题,case函数只返回第一个符合条件的值,剩下的case部分将会被自动忽略。
–比如说,下面这段sql,你永远无法得到“第二类”这个结果

case when col_1 in ( ‘a’, ‘b’) then ‘第一类’
when col_1 in (‘a’) then ‘第二类’
else’其他’ end

下面看一些实例

select country,
sum( case when sex = ‘1’ then
population else 0 end), --男性人口
sum( case when sex = ‘2’ then
population else 0 end) --女性人口
from table_a
group by country;

select sum(population),
case country
when ‘中国’ then ‘亚洲’
when ‘印度’ then ‘亚洲’
when ‘日本’ then ‘亚洲’
when ‘美国’ then ‘北美洲’
when ‘加拿大’ then ‘北美洲’
when ‘墨西哥’ then ‘北美洲’
else ‘其他’ end
from table_a
group by case country
when ‘中国’ then ‘亚洲’
when ‘印度’ then ‘亚洲’
when ‘日本’ then ‘亚洲’
when ‘美国’ then ‘北美洲’
when ‘加拿大’ then ‘北美洲’
when ‘墨西哥’ then ‘北美洲’
else ‘其他’ end;

关于case 二

select
case when salary <= 500 then ‘1’
when salary > 500 and salary <= 600 then ‘2’
when salary > 600 and salary <= 800 then ‘3’
when salary > 800 and salary <= 1000 then ‘4’
else null end salary_class,
count(*)
from table_a
group by
case when salary <= 500 then ‘1’
when salary > 500 and salary <= 600 then ‘2’
when salary > 600 and salary <= 800 then ‘3’
when salary > 800 and salary <= 1000 then ‘4’
else null end;
mysq中横表和纵表的转换有时也是用这种 方法来转换 http://blog.csdn.net/fysuccess/article/details/40789869

博客设置页面,选择一款你喜欢的代码片高亮样式,下面展示同样高亮的 代码片.

SELECT sum(case WHEN coalesce(t1.total_amount,0)-coalesce(t1.deposit,0)-coalesce(t1.freight,0) >=0 then coalesce(t1.total_amount,0) ELSE (coalesce(t1.deposit,0)+coalesce(t1.freight,0)) END)*0.006  as total
  from pat_medical_record_order t1, pay_record t2
WHERE t1.out_trade_no = ANY (t2.out_trade_nos)
and (t1.order_status).o_value in ('已取消','已驳回') and (t1.pay_status).o_value<>'未付款' and t2.pay_method=3
and to_char(t1.update_time,'YYYY-MM')='2019-05'
-- 已取消 已驳回 0.1146
--0.0528
SELECT
    sum(case WHEN coalesce(t1.total_amount,0)-coalesce(t1.deposit,0)-coalesce(t1.freight,0) >=0
    then coalesce(t1.total_amount,0)
           ELSE (case t2.pay_method WHEN 2 THEN coalesce(t1.total_amount,0) ELSE (coalesce(t1.deposit,0)+coalesce(t1.freight,0)) END ) END)*0.006  as total
  from pat_medical_record_order t1 INNER JOIN pay_record t2
      on t1.out_trade_no = ANY (t2.out_trade_nos)
      LEFT JOIN logistics_sheet l ON t1.id=l.order_id
WHERE
 (t1.order_status).o_value='待收货' and l.print_state='1'
and to_char(t1.update_time,'YYYY-MM')='2019-05'
and t1.access='2';
--0.06048
SELECT
    sum(case WHEN coalesce(t1.total_amount,0)-coalesce(t1.deposit,0)-coalesce(t1.freight,0) >=0
    then coalesce(t1.total_amount,0)
           ELSE (case t2.pay_method WHEN 2 THEN coalesce(t1.total_amount,0) ELSE (coalesce(t1.deposit,0)+coalesce(t1.freight,0)) END ) END)*0.006  as total
  from pat_medical_record_order t1 INNER JOIN pay_record t2
      on t1.out_trade_no = ANY (t2.out_trade_nos)
WHERE
 (t1.order_status).o_value='已完成'
and to_char(t1.update_time,'YYYY-MM')='2019-05'
and t1.access='1';



  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值