mysql横向和纵向合并sql数据用于展示

2020年3月11日12:03:47

 使用 UNION 纵向合并两个sql的结果

SELECT DISTINCT(departments) as departments,SUM(final_price) as
        finalPrice,count(*) as orderCount
        FROM phc_order
GROUP BY departments
UNION
SELECT '总计' ,SUM(final_price) as
        finalPrice,count(*) as orderCount
        FROM phc_order

!注意如果上下sql的结果列数要一直不然就会出错

departments finalPrice orderCount
儿科 0.53 69
妇科二 0.61 28
心外科 0.71 64
普外二科 0.43 34
泌尿外二科 0.23 10
烧伤科 0.19 23
科室1 14.06 17
肾血液科 0.07 8
脑血管中心 1.50 39
总计 18.33 292

 

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
使用 JOIN 横向合并两个sql的结果
SELECT A.*, B.*
FROM 
  (SELECT SUM(final_price) as finalPrice,count(*) as orderCount FROM phc_order) as A
  JOIN
  (SELECT SUM(final_price) as finalPrice5,count(*) as orderCount5 FROM phc_order
WHERE order_status = 5) as B

 
注意
finalPrice orderCount finalPrice5 orderCount5
18.33 292 2.21 22

 

 
 
 
快速出sql
SELECT true_name,area_info,address,tel_phone,mob_phone FROM shopnc_address into outfile 'd:/zx.xls'

在配置文件加入,并重启

secure-file-priv=

  

 
 

猜你喜欢

转载自www.cnblogs.com/zx-admin/p/12461459.html