mysql横向和纵向合并sql数据用于展示,快递导出导入海量数据

2020年3月11日12:03:47

MySQL版本5.7

 使用 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的结果列数要一致不然就会出错

departmentsfinalPriceorderCount
儿科 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

 
注意
finalPriceorderCountfinalPrice5orderCount5
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=

  

windows本地测试结果

SELECT * FROM data_international into outfile 'd:/zx.xls';
受影响的行: 436593
时间: 0.523s

TRUNCATE TABLE data_international;
LOAD DATA INFILE 'd:/zx.xls' INTO TABLE data_international; 受影响的行: 436593 时间: 2.453s

  导出导入速度还挺快的

 
SELECT fields INTO OUTFILE 'file_name'
    [{FIELDS | COLUMNS}                     字段
        [TERMINATED BY 'string']                字段之间分隔符号
        [[OPTIONALLY] ENCLOSED BY 'char']       字段被包含在char中间
        [ESCAPED BY 'char']                     忽略字段里出现的char
    ]
    [LINES
        [STARTING BY 'string']              忽略开头是string的行
        [TERMINATED BY 'string']            行分隔符
    ]
FROM test_table;

  

LOAD DATA [LOW_PRIORITY | CONCURRENT] [LOCAL] INFILE 'file_name'
    [REPLACE | IGNORE]                      遇到重复的时候处理方法,替换或者是忽略
    INTO TABLE tbl_name                     导入数据的目的表名
    [PARTITION (partition_name,...)]        分区选择
    [CHARACTER SET charset_name]            字符集
    [{FIELDS | COLUMNS}                     字段
        [TERMINATED BY 'string']                字段之间分隔符号
        [[OPTIONALLY] ENCLOSED BY 'char']       字段被包含在char中间
        [ESCAPED BY 'char']                     忽略字段里出现的char
    ]
    [LINES
        [STARTING BY 'string']              忽略开头是string的行
        [TERMINATED BY 'string']            行分隔符
    ]
    [IGNORE number {LINES | ROWS}]          忽略行/列
    [(col_name_or_user_var,...)]            目的表的表字段名或者用户变量名
    [SET col_name = expr,...]               设置表字段值

  参考文档:https://www.cnblogs.com/wyzs/p/6762452.html

 

posted on 2020-03-11 15:07  zh7314  阅读(2548)  评论(0编辑  收藏  举报