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.5369
妇科二0.6128
心外科0.7164
普外二科0.4334
泌尿外二科0.2310
烧伤科0.1923
科室114.0617
肾血液科0.078
脑血管中心1.5039
总计18.33292
使用 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.332922.2122
快速出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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值