MYSQL语法总结

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了MYSQL语法总结相关的知识,希望对你有一定的参考价值。

# mysql --> 常用脚本汇总
### // 当前时间的timestamp.
new Timestamp(System.currentTimeMillis())

### // 表存在就删除 主键重置:
drop table if exists db_test;

ALTER TABLE sys_user AUTO_INCREMENT= 1;

### // 查看索引 :
explain

### // 数据备份
mysqldump --default-character-set=utf8 -hrm-vy19r48p4g68fac1c.mysql.rds.aliyuncs.com -yinpeizheng -p"[email protected]" customer4paas help_index > help_index.sql

mysqldump --default-character-set=utf8mb4 -hibosserp.mysql.rds.aliyuncs.com -uyuanhuan -pShGZQx1c busicommon bi_int bi_int_chr --where="intact_id=100006270946">yuanh_bi_int.sql

mysql> source order_info1.sql

### // 将某一条记录 置为null
update table set column1 = null;

alter table customer4paas.order_shop drop column goods_num,drop column paid_fee;

alter table customer4paas.order_shop add goods_num bigint(20);
alter table customer4paas.order_shop add paid_fee bigint(20);

### // 黑科技登录 mobile
select * from tenant_info where enterprise_title like ‘%五谷%‘;

### // 统计发货记录 供应商 和 渠道商的维度
select a.tenant_id,count(a.tenant_id),a.create_time from order4paas.order_file_job as a,shop4paas.tenant_info as b where a.job_type=‘2‘ group by a.tenant_id;

select a.tenant_id,a.create_time from order4paas.order_file_job as a,shop4paas.tenant_info as b where a.job_type=‘2‘ group by a.tenant_id order by a.create_time asc;

### // 发货记录按天统计
select count(upload_file),DATE_FORMAT(create_time,"%Y-%m-%d") as groupNum from order4paas.order_file_job where job_type=‘2‘ group by groupNum;

### // 打单记录
select groupNum, count(*) from (
select DATE_FORMAT(q.create_time,"%Y-%m-%d") as groupNum,q.print_id from order4paas.order_print1 as q where state=1 union all
select DATE_FORMAT(w.create_time,"%Y-%m-%d") as groupNum,w.print_id from order4paas.order_print2 as w where state=1 union all
select DATE_FORMAT(e.create_time,"%Y-%m-%d") as groupNum,e.print_id from order4paas.order_print3 as e where state=1 union all
select DATE_FORMAT(r.create_time,"%Y-%m-%d") as groupNum,r.print_id from order4paas.order_print4 as r where state=1 union all
select DATE_FORMAT(t.create_time,"%Y-%m-%d") as groupNum,t.print_id from order4paas.order_print5 as t where state=1 union all
select DATE_FORMAT(y.create_time,"%Y-%m-%d") as groupNum,y.print_id from order4paas.order_print6 as y where state=1 union all
select DATE_FORMAT(u.create_time,"%Y-%m-%d") as groupNum,u.print_id from order4paas.order_print7 as u where state=1 union all
select DATE_FORMAT(i.create_time,"%Y-%m-%d") as groupNum,i.print_id from order4paas.order_print8 as i where state=1 union all
select DATE_FORMAT(o.create_time,"%Y-%m-%d") as groupNum,o.print_id from order4paas.order_print9 as o where state=1 union all
select DATE_FORMAT(p.create_time,"%Y-%m-%d") as groupNum,p.print_id from order4paas.order_print0 as p where state=1) a
group by groupNum order by groupNum asc;

### // 打单类型
select tenant_id,company_name,(case sheet_type when 0 then ‘快递单‘ when 1 then ‘电子面单‘ else ‘出货单‘ end) as type,create_time from order_print0 union all
select tenant_id,company_name,(case sheet_type when 0 then ‘快递单‘ when 1 then ‘电子面单‘ else ‘出货单‘ end) as type,create_time from order_print1 union all
select tenant_id,company_name,(case sheet_type when 0 then ‘快递单‘ when 1 then ‘电子面单‘ else ‘出货单‘ end) as type,create_time from order_print2 union all
select tenant_id,company_name,(case sheet_type when 0 then ‘快递单‘ when 1 then ‘电子面单‘ else ‘出货单‘ end) as type,create_time from order_print3 union all
select tenant_id,company_name,(case sheet_type when 0 then ‘快递单‘ when 1 then ‘电子面单‘ else ‘出货单‘ end) as type,create_time from order_print4 union all
select tenant_id,company_name,(case sheet_type when 0 then ‘快递单‘ when 1 then ‘电子面单‘ else ‘出货单‘ end) as type,create_time from order_print5 union all
select tenant_id,company_name,(case sheet_type when 0 then ‘快递单‘ when 1 then ‘电子面单‘ else ‘出货单‘ end) as type,create_time from order_print6 union all
select tenant_id,company_name,(case sheet_type when 0 then ‘快递单‘ when 1 then ‘电子面单‘ else ‘出货单‘ end) as type,create_time from order_print7 union all
select tenant_id,company_name,(case sheet_type when 0 then ‘快递单‘ when 1 then ‘电子面单‘ else ‘出货单‘ end) as type,create_time from order_print8 union all
select tenant_id,company_name,(case sheet_type when 0 then ‘快递单‘ when 1 then ‘电子面单‘ else ‘出货单‘ end) as type,create_time from order_print9 group by tenant_id;


### // 所有打单统计
select title,mob from (
select a.enterprise_title as title,a.mobile as mob from order4paas.order_print0 as q,shop4paas.tenant_info as a where q.tenant_id = a.tenant_id union all
select s.enterprise_title as title,s.mobile as mob from order4paas.order_print1 as w,shop4paas.tenant_info as s where w.tenant_id = s.tenant_id union all
select d.enterprise_title as title,d.mobile as mob from order4paas.order_print2 as e,shop4paas.tenant_info as d where e.tenant_id = d.tenant_id union all
select f.enterprise_title as title,f.mobile as mob from order4paas.order_print3 as r,shop4paas.tenant_info as f where r.tenant_id = f.tenant_id union all
select g.enterprise_title as title,g.mobile as mob from order4paas.order_print4 as t,shop4paas.tenant_info as g where t.tenant_id = g.tenant_id union all
select h.enterprise_title as title,h.mobile as mob from order4paas.order_print5 as y,shop4paas.tenant_info as h where y.tenant_id = h.tenant_id union all
select j.enterprise_title as title,j.mobile as mob from order4paas.order_print6 as u,shop4paas.tenant_info as j where u.tenant_id = j.tenant_id union all
select k.enterprise_title as title,k.mobile as mob from order4paas.order_print7 as i,shop4paas.tenant_info as k where i.tenant_id = k.tenant_id union all
select l.enterprise_title as title,l.mobile as mob from order4paas.order_print8 as o,shop4paas.tenant_info as l where o.tenant_id = l.tenant_id union all
select z.enterprise_title as title,z.mobile as mob from order4paas.order_print9 as p,shop4paas.tenant_info as z where p.tenant_id = z.tenant_id
) mm group by title;

### // 打单记录
select * from order_print0 where tenant_id =‘100000003284‘ and create_time >‘ 2017-07-31 12:00:00‘
union all select * from order_print1 where tenant_id =‘100000003284‘ and create_time >‘ 2017-07-31 12:00:00‘
union all select * from order_print2 where tenant_id =‘100000003284‘ and create_time >‘ 2017-07-31 12:00:00‘
union all select * from order_print3 where tenant_id =‘100000003284‘ and create_time >‘ 2017-07-31 12:00:00‘
union all select * from order_print4 where tenant_id =‘100000003284‘ and create_time >‘ 2017-07-31 12:00:00‘
union all select * from order_print5 where tenant_id =‘100000003284‘ and create_time >‘ 2017-07-31 12:00:00‘
union all select * from order_print6 where tenant_id =‘100000003284‘ and create_time >‘ 2017-07-31 12:00:00‘
union all select * from order_print7 where tenant_id =‘100000003284‘ and create_time >‘ 2017-07-31 12:00:00‘
union all select * from order_print8 where tenant_id =‘100000003284‘ and create_time >‘ 2017-07-31 12:00:00‘
union all select * from order_print9 where tenant_id =‘100000003284‘ and create_time >‘ 2017-07-31 12:00:00‘


### // 根据物流单号查询 打单记录
select * from order_print0 where delivery_no = ‘535940061920‘
union all select * from order_print1 where delivery_no = ‘535940061920‘
union all select * from order_print2 where delivery_no = ‘535940061920‘
union all select * from order_print3 where delivery_no = ‘535940061920‘
union all select * from order_print4 where delivery_no = ‘535940061920‘
union all select * from order_print5 where delivery_no = ‘535940061920‘
union all select * from order_print6 where delivery_no = ‘535940061920‘
union all select * from order_print7 where delivery_no = ‘535940061920‘
union all select * from order_print8 where delivery_no = ‘535940061920‘
union all select * from order_print9 where delivery_no = ‘535940061920‘
order by create_time desc limit 10;

### 按月
select count(*) as totalNum,sum(paid_fee) as paidFee,DATE_FORMAT(create_time,"%Y-%m") as dataF from order_info0 where DATE_SUB(CURDATE(), INTERVAL 120 DAY) <= date(create_time) and state = 1 group by dataF;

### 每日供应商 和 渠道商 分别得个数
select a.count,b.count,a.dataF from (
(select tenant_type, count(*) as count,DATE_FORMAT(create_time,"%Y-%m-%d") as dataF from tenant_info where tenant_type=0 and state=1 group by dataF) a,
(select tenant_type, count(*) as count,DATE_FORMAT(create_time,"%Y-%m-%d") as dataF from tenant_info where tenant_type=1 and state=1 group by dataF) b
) where a.dataF=b.dataF;

### 今日新增
select count(*) from tenant_info where to_days(create_time) = to_days(now()) and state =1;

### 昨日新增
select count(*) from tenant_info where to_days(now()) - to_days(create_time) = 1 and state =1;

### 查看哪个店铺订单多
db.order_fetch_logs.find({})

### 查询最近30天的 客户增长
select count(*) ,DATE_FORMAT(create_time,"%Y-%m-%d") as dataF from customer_info1 where DATE_SUB(CURDATE(), INTERVAL 30 DAY) <= date(create_time) group by dataF;


### 查询 重复打印的订单
select order_id as ‘系统订单号‘,delivery_no as ‘快递单号‘,count(*) as count from order_print3 where print_tenant_id =‘100000016563‘ and create_time > ‘2017-8-16 00:00:00‘ group by delivery_no having count > 1;

### 订单 金额 曲线图.
select count(*) as totalNum,sum(paid_fee) as paidFee,DATE_FORMAT(create_time,"%Y-%m-%d") as dataF from order_infoX where DATE_SUB(CURDATE(), INTERVAL 90 DAY) <= date(create_time) and DATE_SUB(CURDATE(), INTERVAL 1 DAY) >= date(create_time) and state = 1 group by dataF;

### 统计个数据:日均200单以上的商家数,日均100单的商家数,日订单50——100的商家数,订单日均50以下的商家数

select count(*) as num,count(*)*1.0/30 as avg,tenant_id from order_info0 where DATE_SUB(CURDATE(), INTERVAL 30 DAY) <= date(create_time) and DATE_SUB(CURDATE(), INTERVAL 1 DAY) >= date(create_time) and state = 1 group by tenant_id order by num;
select count(*) as num,count(*)*1.0/30 as avg,tenant_id from order_info1 where DATE_SUB(CURDATE(), INTERVAL 30 DAY) <= date(create_time) and DATE_SUB(CURDATE(), INTERVAL 1 DAY) >= date(create_time) and state = 1 group by tenant_id order by num;
select count(*) as num,count(*)*1.0/30 as avg,tenant_id from order_info2 where DATE_SUB(CURDATE(), INTERVAL 30 DAY) <= date(create_time) and DATE_SUB(CURDATE(), INTERVAL 1 DAY) >= date(create_time) and state = 1 group by tenant_id order by num;
select count(*) as num,count(*)*1.0/30 as avg,tenant_id from order_info3 where DATE_SUB(CURDATE(), INTERVAL 30 DAY) <= date(create_time) and DATE_SUB(CURDATE(), INTERVAL 1 DAY) >= date(create_time) and state = 1 group by tenant_id order by num;
select count(*) as num,count(*)*1.0/30 as avg,tenant_id from order_info4 where DATE_SUB(CURDATE(), INTERVAL 30 DAY) <= date(create_time) and DATE_SUB(CURDATE(), INTERVAL 1 DAY) >= date(create_time) and state = 1 group by tenant_id order by num;
select count(*) as num,count(*)*1.0/30 as avg,tenant_id from order_info5 where DATE_SUB(CURDATE(), INTERVAL 30 DAY) <= date(create_time) and DATE_SUB(CURDATE(), INTERVAL 1 DAY) >= date(create_time) and state = 1 group by tenant_id order by num;
select count(*) as num,count(*)*1.0/30 as avg,tenant_id from order_info6 where DATE_SUB(CURDATE(), INTERVAL 30 DAY) <= date(create_time) and DATE_SUB(CURDATE(), INTERVAL 1 DAY) >= date(create_time) and state = 1 group by tenant_id order by num;
select count(*) as num,count(*)*1.0/30 as avg,tenant_id from order_info7 where DATE_SUB(CURDATE(), INTERVAL 30 DAY) <= date(create_time) and DATE_SUB(CURDATE(), INTERVAL 1 DAY) >= date(create_time) and state = 1 group by tenant_id order by num;
select count(*) as num,count(*)*1.0/30 as avg,tenant_id from order_info8 where DATE_SUB(CURDATE(), INTERVAL 30 DAY) <= date(create_time) and DATE_SUB(CURDATE(), INTERVAL 1 DAY) >= date(create_time) and state = 1 group by tenant_id order by num;
select count(*) as num,count(*)*1.0/30 as avg,tenant_id from order_info9 where DATE_SUB(CURDATE(), INTERVAL 30 DAY) <= date(create_time) and DATE_SUB(CURDATE(), INTERVAL 1 DAY) >= date(create_time) and state = 1 group by tenant_id order by num;

### 查询 每日渠道商和供应商 数量
select a.count,b.count,a.dataF from ((select tenant_type, count(*) as count,DATE_FORMAT(create_time,"%Y-%m-%d") as dataF from tenant_info where tenant_type=0 and state=1 group by dataF) a,(select tenant_type, count(*) as count,DATE_FORMAT(create_time,"%Y-%m-%d") as dataF from tenant_info where tenant_type=1 and state=1 group by dataF) b) where a.dataF=b.dataF;


### 查询所有 渠道以及数量
select platform_id,count(1) from platform_shop_info group by platform_id;

### 查询每个省的 客户以及数量
select left(place,2),(case left(place,2) when ‘内蒙‘ then ‘内蒙古‘ when ‘黑龙‘ then ‘黑龙江‘ else left(place,2) end),count(left(place,2)) from cust_visit_record where focus_point =‘手机归属地‘ and state=1 group by left(place,2);

### 获取所有渠道 所有省的总客户数 --> 租户视角
public List<Map<String, Object>> getCustProNum() {
StringBuilder sql = new StringBuilder();
sql.append("select left(place,2),(case left(place,2) when ‘内蒙‘ then ‘内蒙古‘ when ‘黑龙‘ then ‘黑龙江‘ else left(place,2) end),count(left(place,2)) ");
sql.append("from cust_visit_record ");
sql.append("where focus_point =‘手机归属地‘ ");
sql.append("and state = 1 ");
sql.append("group by left(place,2) ");

Map<String, Object> paramMap = new HashMap<>();
paramMap.put("state", CommonConst.State.available);

return jdbcTemplate.queryForList(sql.toString(), paramMap);
}


select count(*) as totalNum,sum(paid_fee) as paidFee,DATE_FORMAT(create_time,"%Y-%m-%d") as dataF from order_info0 where DATE_SUB(CURDATE(), INTERVAL 90 DAY) <= date(create_time)
and DATE_SUB(CURDATE(), INTERVAL 0 DAY) >= date(create_time) and state = 1 group by dataF;


### Mysql分组 然后按照字符串拼接

2. select mobile,enterprise_title,(case tenant_type when 0 then ‘渠道商‘ when 1 then ‘供应商‘ else ‘二者均是‘ end) as type from tenant_info where state=1 order by tenant_type;

3. 多表查询连接 实例:
select enterprise_title,mobile
from shop4paas.tenant_info
where tenant_id in
(select distinct tenant_id from order4paas.order_logistics0
union all
select distinct tenant_id from order4paas.order_logistics1
union all
select distinct tenant_id from order4paas.order_logistics2
union all
select distinct tenant_id from order4paas.order_logistics3
union all
select distinct tenant_id from order4paas.order_logistics4
union all
select distinct tenant_id from order4paas.order_logistics5
union all
select distinct tenant_id from order4paas.order_logistics6
union all
select distinct tenant_id from order4paas.order_logistics7
union all
select distinct tenant_id from order4paas.order_logistics8
union all
select distinct tenant_id from order4paas.order_logistics9
);
4. select enterprise_title,mobile from shop4paas.tenant_info where tenant_id in (select distinct tenant_id from order4paas.order_file_job where job_type=‘2‘ );

5. select * from sys_operator where op_id=‘100000000004‘; 查看opId是谁!!

6. select * from order_info8 where ext_order_id ="E20170518215943034787658";

7. // 小亚通客户活跃时间 ,以发货作为判断标准 (\G是不需要加分号的哦)
select j.tenant_id,t.enterprise_title,j.upload_file,j.create_time,j.job_status,j.create_op from order4paas.order_file_job j,shop4paas.tenant_info t where j.tenant_id = t.tenant_id and j.job_type=‘1‘ and j.create_time >‘2017-06-01 00:00:00‘ group by tenant_id;

//自己的
DROP TABLE IF EXISTS sender_info;

8.单点发货的:

db.hub_api_call_log.aggregate( [
{ $match: { apiCode: "delivery" } },
{ $group: { _id: { tenantId: "$tenantId"}, count: { $sum: 1 } } },
{ $match: { count: { $gt: 1 } } },
{ $project: { _id: 0, tenantId: "$_id.tenantId", count: 1}}
] );


db.hub_api_call_log.find({"apiCode":"delivery","tenantId":100000000407})
9. 根据租户名称 查出手机号 黑科技登录上去

select enterprise_title,create_time,mobile from tenant_info where enterprise_title like‘%罗辑思维%‘;

10. sysmgnt4paas.sys_operator 根据tenant_id 查看 op_id

19. //以前两个字符为准,所有的快递公司
select count(*),company_name from (
select left(company_name,2) company_name from order_logistics0 union all
select left(company_name,2) company_name from order_logistics1 union all
select left(company_name,2) company_name from order_logistics2 union all
select left(company_name,2) company_name from order_logistics3 union all
select left(company_name,2) company_name from order_logistics4 union all
select left(company_name,2) company_name from order_logistics5 union all
select left(company_name,2) company_name from order_logistics6 union all
select left(company_name,2) company_name from order_logistics7 union all
select left(company_name,2) company_name from order_logistics8 union all
select left(company_name,2) company_name from order_logistics9) a
group by company_name order by 1;

//跟映射表关联
select count(*),company_name from (
select company_name from order4paas.order_logistics0 union all
select company_name from order4paas.order_logistics1 union all
select company_name from order4paas.order_logistics2 union all
select company_name from order4paas.order_logistics3 union all
select company_name from order4paas.order_logistics4 union all
select company_name from order4paas.order_logistics5 union all
select company_name from order4paas.order_logistics6 union all
select company_name from order4paas.order_logistics7 union all
select company_name from order4paas.order_logistics8 union all
select company_name from order4paas.order_logistics9) a
where company_name in (select logistics_company_name from warehouse4paas.logistics_company_mapping)
group by company_name order by 1;

20.// 增加删除修改 字段信息
alter table sys_notice modify content varchar(255);

alter table sysmgnt4paas.sender_info add column monthly_coding varchar(32);

alter table customer4paas.order_shop drop column goods_num,drop column paid_fee;

alter table customer4paas.order_shop add goods_num bigint(20);
21.
// 抓取订单
grep "众鼎云端" app_RabbitProducer.log

// 抓取商品
grep "圣手丹医膏药贴" app_RabbitConsumer.log
22.
select tenant_id,company_name,(case sheet_type when 0 then ‘快递单‘ when 1 then ‘电子面单‘ else ‘出货单‘ end) as type,create_time from order_print0
union all select tenant_id,company_name,(case sheet_type when 0 then ‘快递单‘ when 1 then ‘电子面单‘ else ‘出货单‘ end) as type,create_time from order_print1
union all select tenant_id,company_name,(case sheet_type when 0 then ‘快递单‘ when 1 then ‘电子面单‘ else ‘出货单‘ end) as type,create_time from order_print2
union all select tenant_id,company_name,(case sheet_type when 0 then ‘快递单‘ when 1 then ‘电子面单‘ else ‘出货单‘ end) as type,create_time from order_print3
union all select tenant_id,company_name,(case sheet_type when 0 then ‘快递单‘ when 1 then ‘电子面单‘ else ‘出货单‘ end) as type,create_time from order_print4
union all select tenant_id,company_name,(case sheet_type when 0 then ‘快递单‘ when 1 then ‘电子面单‘ else ‘出货单‘ end) as type,create_time from order_print5
union all select tenant_id,company_name,(case sheet_type when 0 then ‘快递单‘ when 1 then ‘电子面单‘ else ‘出货单‘ end) as type,create_time from order_print6
union all select tenant_id,company_name,(case sheet_type when 0 then ‘快递单‘ when 1 then ‘电子面单‘ else ‘出货单‘ end) as type,create_time from order_print7
union all select tenant_id,company_name,(case sheet_type when 0 then ‘快递单‘ when 1 then ‘电子面单‘ else ‘出货单‘ end) as type,create_time from order_print8
union all select tenant_id,company_name,(case sheet_type when 0 then ‘快递单‘ when 1 then ‘电子面单‘ else ‘出货单‘ end) as type,create_time from order_print9
group by tenant_id;


23.// 近半个月的销量
select allSkus.sku_title as sku名称,allSkus.sku_id as sku编号,allSkus.num as 销量,allSkus.paid_fee as 销售额,allSkus.channel as 渠道商,allSkus.supplier as 供应商 from ( SELECT i.sku_title, i.sku_id, SUM(i.num) as num,cast(SUM(i.paid_fee)/100 as decimal(18,2)) as paid_fee, t1.enterprise_title as channel, t2.enterprise_title as supplier FROM order4paas.order_items0 i, order4paas.order_info0 o, shop4paas.tenant_info t1, shop4paas.tenant_info t2 WHERE i.order_id = o.order_id AND o.tenant_id = t1.tenant_id AND o.supplier_tenant_id = t2.tenant_id AND i.state = 1 AND o.state = 1 AND t1.state = 1 AND t2.state = 1 AND DATE_SUB(CURDATE(), INTERVAL 14 DAY) <= DATE(o.pay_time) GROUP BY sku_id union SELECT i.sku_title, i.sku_id, SUM(i.num) as num,cast(SUM(i.paid_fee)/100 as decimal(18,2)) as paid_fee, t1.enterprise_title as channel, t2.enterprise_title as supplier FROM order4paas.order_items1 i, order4paas.order_info1 o, shop4paas.tenant_info t1, shop4paas.tenant_info t2 WHERE i.order_id = o.order_id AND o.tenant_id = t1.tenant_id AND o.supplier_tenant_id = t2.tenant_id AND i.state = 1 AND o.state = 1 AND t1.state = 1 AND t2.state = 1 AND DATE_SUB(CURDATE(), INTERVAL 14 DAY) <= DATE(o.pay_time) GROUP BY sku_id union SELECT i.sku_title, i.sku_id, SUM(i.num) as num,cast(SUM(i.paid_fee)/100 as decimal(18,2)) as paid_fee, t1.enterprise_title as channel, t2.enterprise_title as supplier FROM order4paas.order_items2 i, order4paas.order_info2 o, shop4paas.tenant_info t1, shop4paas.tenant_info t2 WHERE i.order_id = o.order_id AND o.tenant_id = t1.tenant_id AND o.supplier_tenant_id = t2.tenant_id AND i.state = 1 AND o.state = 1 AND t1.state = 1 AND t2.state = 1 AND DATE_SUB(CURDATE(), INTERVAL 14 DAY) <= DATE(o.pay_time) GROUP BY sku_id union SELECT i.sku_title, i.sku_id, SUM(i.num) as num,cast(SUM(i.paid_fee)/100 as decimal(18,2)) as paid_fee, t1.enterprise_title as channel, t2.enterprise_title as supplier FROM order4paas.order_items3 i, order4paas.order_info3 o, shop4paas.tenant_info t1, shop4paas.tenant_info t2 WHERE i.order_id = o.order_id AND o.tenant_id = t1.tenant_id AND o.supplier_tenant_id = t2.tenant_id AND i.state = 1 AND o.state = 1 AND t1.state = 1 AND t2.state = 1 AND DATE_SUB(CURDATE(), INTERVAL 14 DAY) <= DATE(o.pay_time) GROUP BY sku_id union SELECT i.sku_title, i.sku_id, SUM(i.num) as num,cast(SUM(i.paid_fee)/100 as decimal(18,2)) as paid_fee, t1.enterprise_title as channel, t2.enterprise_title as supplier FROM order4paas.order_items4 i, order4paas.order_info4 o, shop4paas.tenant_info t1, shop4paas.tenant_info t2 WHERE i.order_id = o.order_id AND o.tenant_id = t1.tenant_id AND o.supplier_tenant_id = t2.tenant_id AND i.state = 1 AND o.state = 1 AND t1.state = 1 AND t2.state = 1 AND DATE_SUB(CURDATE(), INTERVAL 14 DAY) <= DATE(o.pay_time) GROUP BY sku_id union SELECT i.sku_title, i.sku_id, SUM(i.num) as num,cast(SUM(i.paid_fee)/100 as decimal(18,2)) as paid_fee, t1.enterprise_title as channel, t2.enterprise_title as supplier FROM order4paas.order_items5 i, order4paas.order_info5 o, shop4paas.tenant_info t1, shop4paas.tenant_info t2 WHERE i.order_id = o.order_id AND o.tenant_id = t1.tenant_id AND o.supplier_tenant_id = t2.tenant_id AND i.state = 1 AND o.state = 1 AND t1.state = 1 AND t2.state = 1 AND DATE_SUB(CURDATE(), INTERVAL 14 DAY) <= DATE(o.pay_time) GROUP BY sku_id union SELECT i.sku_title, i.sku_id, SUM(i.num) as num,cast(SUM(i.paid_fee)/100 as decimal(18,2)) as paid_fee, t1.enterprise_title as channel, t2.enterprise_title as supplier FROM order4paas.order_items6 i, order4paas.order_info6 o, shop4paas.tenant_info t1, shop4paas.tenant_info t2 WHERE i.order_id = o.order_id AND o.tenant_id = t1.tenant_id AND o.supplier_tenant_id = t2.tenant_id AND i.state = 1 AND o.state = 1 AND t1.state = 1 AND t2.state = 1 AND DATE_SUB(CURDATE(), INTERVAL 14 DAY) <= DATE(o.pay_time) GROUP BY sku_id union SELECT i.sku_title, i.sku_id, SUM(i.num) as num,cast(SUM(i.paid_fee)/100 as decimal(18,2)) as paid_fee, t1.enterprise_title as channel, t2.enterprise_title as supplier FROM order4paas.order_items7 i, order4paas.order_info7 o, shop4paas.tenant_info t1, shop4paas.tenant_info t2 WHERE i.order_id = o.order_id AND o.tenant_id = t1.tenant_id AND o.supplier_tenant_id = t2.tenant_id AND i.state = 1 AND o.state = 1 AND t1.state = 1 AND t2.state = 1 AND DATE_SUB(CURDATE(), INTERVAL 14 DAY) <= DATE(o.pay_time) GROUP BY sku_id union SELECT i.sku_title, i.sku_id, SUM(i.num) as num,cast(SUM(i.paid_fee)/100 as decimal(18,2)) as paid_fee, t1.enterprise_title as channel, t2.enterprise_title as supplier FROM order4paas.order_items8 i, order4paas.order_info8 o, shop4paas.tenant_info t1, shop4paas.tenant_info t2 WHERE i.order_id = o.order_id AND o.tenant_id = t1.tenant_id AND o.supplier_tenant_id = t2.tenant_id AND i.state = 1 AND o.state = 1 AND t1.state = 1 AND t2.state = 1 AND DATE_SUB(CURDATE(), INTERVAL 14 DAY) <= DATE(o.pay_time) GROUP BY sku_id union SELECT i.sku_title, i.sku_id, SUM(i.num) as num,cast(SUM(i.paid_fee)/100 as decimal(18,2)) as paid_fee, t1.enterprise_title as channel, t2.enterprise_title as supplier FROM order4paas.order_items9 i, order4paas.order_info9 o, shop4paas.tenant_info t1, shop4paas.tenant_info t2 WHERE i.order_id = o.order_id AND o.tenant_id = t1.tenant_id AND o.supplier_tenant_id = t2.tenant_id AND i.state = 1 AND o.state = 1 AND t1.state = 1 AND t2.state = 1 AND DATE_SUB(CURDATE(), INTERVAL 14 DAY) <= DATE(o.pay_time) GROUP BY sku_id ) allSkus order by allSkus.num desc limit 200


// 导入订单
select * from order_file_job where job_type=0 order by create_time desc limit 10\G

24.// 清除全部 redis

delkey 9378 *api*

25.// 使注册的手机号失效

UPDATE `sysmgnt4paas`.`sys_operator` SET `state`=‘0‘ WHERE login_name = ‘15858151509‘;

26.
根据商城订单号查询订单是否在小亚通发货、以及操作日志,可用这样的sql:
select * from order_info0 where ext_order_id=‘100700047708901‘;
select * from order_operation_logs0 where order_id=100003618817 order by create_time;
select * from order_logistics0 where order_id in (select order_id from order_info0 where ext_order_id in (‘100700047708901‘,‘100700049740602‘,‘100700045776301‘,‘100700045735801‘,‘100700047759802‘,‘100700046777502‘));


29.
// 打单统计按天
select groupNum, count(*) from (
select DATE_FORMAT(q.create_time,"%Y-%m-%d") as groupNum,q.print_id from order4paas.order_print1 as q where state=1 union all
select DATE_FORMAT(w.create_time,"%Y-%m-%d") as groupNum,w.print_id from order4paas.order_print2 as w where state=1 union all
select DATE_FORMAT(e.create_time,"%Y-%m-%d") as groupNum,e.print_id from order4paas.order_print3 as e where state=1 union all
select DATE_FORMAT(r.create_time,"%Y-%m-%d") as groupNum,r.print_id from order4paas.order_print4 as r where state=1 union all
select DATE_FORMAT(t.create_time,"%Y-%m-%d") as groupNum,t.print_id from order4paas.order_print5 as t where state=1 union all
select DATE_FORMAT(y.create_time,"%Y-%m-%d") as groupNum,y.print_id from order4paas.order_print6 as y where state=1 union all
select DATE_FORMAT(u.create_time,"%Y-%m-%d") as groupNum,u.print_id from order4paas.order_print7 as u where state=1 union all
select DATE_FORMAT(i.create_time,"%Y-%m-%d") as groupNum,i.print_id from order4paas.order_print8 as i where state=1 union all
select DATE_FORMAT(o.create_time,"%Y-%m-%d") as groupNum,o.print_id from order4paas.order_print9 as o where state=1 union all
select DATE_FORMAT(p.create_time,"%Y-%m-%d") as groupNum,p.print_id from order4paas.order_print0 as p where state=1) a
group by groupNum order by groupNum asc;


30.
// 按省份 统计订单数量
select left(receiver_state,2),(case left(receiver_state,2) when ‘内蒙‘ then ‘内蒙古‘ when ‘黑龙‘ then ‘黑龙江‘ else left(receiver_state,2) end),count(left(receiver_state,2)) from order_consignee0 group by left(receiver_state,2);

31.
// 每日供应商 和 渠道商 分别得个数
select a.count,b.count,a.dataF from ((select tenant_type, count(*) as count,DATE_FORMAT(create_time,"%Y-%m-%d") as dataF from tenant_info where tenant_type=0 and state=1 group by dataF) a,(select tenant_type, count(*) as count,DATE_FORMAT(create_time,"%Y-%m-%d") as dataF from tenant_info where tenant_type=1 and state=1 group by dataF) b) where a.dataF=b.dataF;

32.
// 查询系统近三个月的订单 和 金额
// 查询系统近三个月的订单 和 金额
select sum(totalNum),sum(totalFee),dataF from (
select count(*) as totalNum,sum(total_fee) as totalFee,DATE_FORMAT(create_time,"%Y-%m-%d") as dataF from order_info0 where DATE_SUB(CURDATE(), INTERVAL 59 DAY) <= date(create_time) and DATE_SUB(CURDATE(), INTERVAL 29 DAY) >= date(create_time) and order_status != 999 and state = 1 and tenant_id < ‘101000000000‘ group by dataF union all
select count(*) as totalNum,sum(total_fee) as totalFee,DATE_FORMAT(create_time,"%Y-%m-%d") as dataF from order_info1 where DATE_SUB(CURDATE(), INTERVAL 59 DAY) <= date(create_time) and DATE_SUB(CURDATE(), INTERVAL 29 DAY) >= date(create_time) and order_status != 999 and state = 1 and tenant_id < ‘101000000000‘ group by dataF union all
select count(*) as totalNum,sum(total_fee) as totalFee,DATE_FORMAT(create_time,"%Y-%m-%d") as dataF from order_info2 where DATE_SUB(CURDATE(), INTERVAL 59 DAY) <= date(create_time) and DATE_SUB(CURDATE(), INTERVAL 29 DAY) >= date(create_time) and order_status != 999 and state = 1 and tenant_id < ‘101000000000‘ group by dataF union all
select count(*) as totalNum,sum(total_fee) as totalFee,DATE_FORMAT(create_time,"%Y-%m-%d") as dataF from order_info3 where DATE_SUB(CURDATE(), INTERVAL 59 DAY) <= date(create_time) and DATE_SUB(CURDATE(), INTERVAL 29 DAY) >= date(create_time) and order_status != 999 and state = 1 and tenant_id < ‘101000000000‘ group by dataF union all
select count(*) as totalNum,sum(total_fee) as totalFee,DATE_FORMAT(create_time,"%Y-%m-%d") as dataF from order_info4 where DATE_SUB(CURDATE(), INTERVAL 59 DAY) <= date(create_time) and DATE_SUB(CURDATE(), INTERVAL 29 DAY) >= date(create_time) and order_status != 999 and state = 1 and tenant_id < ‘101000000000‘ group by dataF union all
select count(*) as totalNum,sum(total_fee) as totalFee,DATE_FORMAT(create_time,"%Y-%m-%d") as dataF from order_info5 where DATE_SUB(CURDATE(), INTERVAL 59 DAY) <= date(create_time) and DATE_SUB(CURDATE(), INTERVAL 29 DAY) >= date(create_time) and order_status != 999 and state = 1 and tenant_id < ‘101000000000‘ group by dataF union all
select count(*) as totalNum,sum(total_fee) as totalFee,DATE_FORMAT(create_time,"%Y-%m-%d") as dataF from order_info6 where DATE_SUB(CURDATE(), INTERVAL 59 DAY) <= date(create_time) and DATE_SUB(CURDATE(), INTERVAL 29 DAY) >= date(create_time) and order_status != 999 and state = 1 and tenant_id < ‘101000000000‘ group by dataF union all
select count(*) as totalNum,sum(total_fee) as totalFee,DATE_FORMAT(create_time,"%Y-%m-%d") as dataF from order_info7 where DATE_SUB(CURDATE(), INTERVAL 59 DAY) <= date(create_time) and DATE_SUB(CURDATE(), INTERVAL 29 DAY) >= date(create_time) and order_status != 999 and state = 1 and tenant_id < ‘101000000000‘ group by dataF union all
select count(*) as totalNum,sum(total_fee) as totalFee,DATE_FORMAT(create_time,"%Y-%m-%d") as dataF from order_info8 where DATE_SUB(CURDATE(), INTERVAL 59 DAY) <= date(create_time) and DATE_SUB(CURDATE(), INTERVAL 29 DAY) >= date(create_time) and order_status != 999 and state = 1 and tenant_id < ‘101000000000‘ group by dataF union all
select count(*) as totalNum,sum(total_fee) as totalFee,DATE_FORMAT(create_time,"%Y-%m-%d") as dataF from order_info9 where DATE_SUB(CURDATE(), INTERVAL 59 DAY) <= date(create_time) and DATE_SUB(CURDATE(), INTERVAL 29 DAY) >= date(create_time) and order_status != 999 and state = 1 and tenant_id < ‘101000000000‘ group by dataF
) as a group by dataF;

32.
// 8月发货排行榜
select count(*),a.tenant_id,b.enterprise_title from order4paas.order_file_job as a,shop4paas.tenant_info as b where a.create_time > ‘2017-8-01 00:00:00‘ and a.create_time < ‘2017-8-31 23:59:59‘ and a.job_type = 2 and a.tenant_id = b.tenant_id group by a.tenant_id order by count(*) desc;

// 8月打单排行榜
select print_tenant_id,enterprise_title,sum(num) from (
select a.print_tenant_id,b.enterprise_title,count(*) as num from order4paas.order_print0 as a,shop4paas.tenant_info as b where a.create_time > ‘2017-8-01 00:00:00‘ and a.create_time < ‘2017-8-31 23:59:59‘ and a.print_tenant_id = b.tenant_id group by a.print_tenant_id union all
select a.print_tenant_id,b.enterprise_title,count(*) as num from order4paas.order_print1 as a,shop4paas.tenant_info as b where a.create_time > ‘2017-8-01 00:00:00‘ and a.create_time < ‘2017-8-31 23:59:59‘ and a.print_tenant_id = b.tenant_id group by a.print_tenant_id union all
select a.print_tenant_id,b.enterprise_title,count(*) as num from order4paas.order_print2 as a,shop4paas.tenant_info as b where a.create_time > ‘2017-8-01 00:00:00‘ and a.create_time < ‘2017-8-31 23:59:59‘ and a.print_tenant_id = b.tenant_id group by a.print_tenant_id union all
select a.print_tenant_id,b.enterprise_title,count(*) as num from order4paas.order_print3 as a,shop4paas.tenant_info as b where a.create_time > ‘2017-8-01 00:00:00‘ and a.create_time < ‘2017-8-31 23:59:59‘ and a.print_tenant_id = b.tenant_id group by a.print_tenant_id union all
select a.print_tenant_id,b.enterprise_title,count(*) as num from order4paas.order_print4 as a,shop4paas.tenant_info as b where a.create_time > ‘2017-8-01 00:00:00‘ and a.create_time < ‘2017-8-31 23:59:59‘ and a.print_tenant_id = b.tenant_id group by a.print_tenant_id union all
select a.print_tenant_id,b.enterprise_title,count(*) as num from order4paas.order_print5 as a,shop4paas.tenant_info as b where a.create_time > ‘2017-8-01 00:00:00‘ and a.create_time < ‘2017-8-31 23:59:59‘ and a.print_tenant_id = b.tenant_id group by a.print_tenant_id union all
select a.print_tenant_id,b.enterprise_title,count(*) as num from order4paas.order_print6 as a,shop4paas.tenant_info as b where a.create_time > ‘2017-8-01 00:00:00‘ and a.create_time < ‘2017-8-31 23:59:59‘ and a.print_tenant_id = b.tenant_id group by a.print_tenant_id union all
select a.print_tenant_id,b.enterprise_title,count(*) as num from order4paas.order_print7 as a,shop4paas.tenant_info as b where a.create_time > ‘2017-8-01 00:00:00‘ and a.create_time < ‘2017-8-31 23:59:59‘ and a.print_tenant_id = b.tenant_id group by a.print_tenant_id union all
select a.print_tenant_id,b.enterprise_title,count(*) as num from order4paas.order_print8 as a,shop4paas.tenant_info as b where a.create_time > ‘2017-8-01 00:00:00‘ and a.create_time < ‘2017-8-31 23:59:59‘ and a.print_tenant_id = b.tenant_id group by a.print_tenant_id union all
select a.print_tenant_id,b.enterprise_title,count(*) as num from order4paas.order_print9 as a,shop4paas.tenant_info as b where a.create_time > ‘2017-8-01 00:00:00‘ and a.create_time < ‘2017-8-31 23:59:59‘ and a.print_tenant_id = b.tenant_id group by a.print_tenant_id
) z group by print_tenant_id order by sum(num) desc;

33.
// 既是渠道商 又是供应商 又有有效店铺的租户
SELECT * FROM tenant_info WHERE tenant_id IN (SELECT channel_tenant_id FROM tenant_association WHERE state=1) AND tenant_id IN (SELECT supplier_tenant_id FROM tenant_association WHERE state=1)
AND tenant_id IN (SELECT tenant_id FROM platform_shop_info WHERE state =1 ) AND state =1;

// 黑科技订单
sql.append("and tenant_id NOT IN ( 101000000000,101000000001,101000000002,101000000003,101000000004,101000000005,101000000006,101000000007,101000000008,101000000009 ) ");

// 黑科技租户
select count(*) from tenant_info where tenant_id >= ‘101000000000‘ and create_time < sysdate();

34.
// Mysql or 对 甲乙合同双方可见!
CriteriaEx sqlCond = CriteriaEx.parseQueryCondition(param2CondMap, condition);
sqlCond.addEqual(ElecAgreementBean.S_State,CommonConst.State.available);

Criteria.Criterion criterion = sqlCond.getNewCriterionForEqual(ElecAgreementBean.S_FirstTenant, SessionContext.getTenantId());
criterion.or(sqlCond.getNewCriterionForEqual(ElecAgreementBean.S_SecondTenant, SessionContext.getTenantId()));
sqlCond.add(criterion);

35.
// 某个时间段内的所有订单数量:
SELECT SUM(a) FROM (
SELECT COUNT(*) AS a FROM order_info0 WHERE create_time >‘ 2017-09-01 00:00:00‘ AND create_time <‘ 2017-09-28 00:00:00‘ UNION ALL
SELECT COUNT(*) AS a FROM order_info1 WHERE create_time >‘ 2017-09-01 00:00:00‘ AND create_time <‘ 2017-09-28 00:00:00‘ UNION ALL
SELECT COUNT(*) AS a FROM order_info2 WHERE create_time >‘ 2017-09-01 00:00:00‘ AND create_time <‘ 2017-09-28 00:00:00‘ UNION ALL
SELECT COUNT(*) AS a FROM order_info3 WHERE create_time >‘ 2017-09-01 00:00:00‘ AND create_time <‘ 2017-09-28 00:00:00‘ UNION ALL
SELECT COUNT(*) AS a FROM order_info4 WHERE create_time >‘ 2017-09-01 00:00:00‘ AND create_time <‘ 2017-09-28 00:00:00‘ UNION ALL
SELECT COUNT(*) AS a FROM order_info5 WHERE create_time >‘ 2017-09-01 00:00:00‘ AND create_time <‘ 2017-09-28 00:00:00‘ UNION ALL
SELECT COUNT(*) AS a FROM order_info6 WHERE create_time >‘ 2017-09-01 00:00:00‘ AND create_time <‘ 2017-09-28 00:00:00‘ UNION ALL
SELECT COUNT(*) AS a FROM order_info7 WHERE create_time >‘ 2017-09-01 00:00:00‘ AND create_time <‘ 2017-09-28 00:00:00‘ UNION ALL
SELECT COUNT(*) AS a FROM order_info8 WHERE create_time >‘ 2017-09-01 00:00:00‘ AND create_time <‘ 2017-09-28 00:00:00‘ UNION al;l
SELECT COUNT(*) AS a FROM order_info9 WHERE create_time >‘ 2017-09-01 00:00:00‘ AND create_time <‘ 2017-09-28 00:00:00‘
) b;

// 某个时间段内的所有销售金额:
SELECT SUM(a) FROM (
SELECT SUM(total_fee) AS a FROM order_info0 WHERE create_time >‘ 2017-09-01 00:00:00‘ AND create_time <‘ 2017-09-28 00:00:00‘ UNION ALL
SELECT SUM(total_fee) AS a FROM order_info1 WHERE create_time >‘ 2017-09-01 00:00:00‘ AND create_time <‘ 2017-09-28 00:00:00‘ UNION ALL
SELECT SUM(total_fee) AS a FROM order_info2 WHERE create_time >‘ 2017-09-01 00:00:00‘ AND create_time <‘ 2017-09-28 00:00:00‘ UNION ALL
SELECT SUM(total_fee) AS a FROM order_info3 WHERE create_time >‘ 2017-09-01 00:00:00‘ AND create_time <‘ 2017-09-28 00:00:00‘ UNION ALL
SELECT SUM(total_fee) AS a FROM order_info4 WHERE create_time >‘ 2017-09-01 00:00:00‘ AND create_time <‘ 2017-09-28 00:00:00‘ UNION ALL
SELECT SUM(total_fee) AS a FROM order_info5 WHERE create_time >‘ 2017-09-01 00:00:00‘ AND create_time <‘ 2017-09-28 00:00:00‘ UNION ALL
SELECT SUM(total_fee) AS a FROM order_info6 WHERE create_time >‘ 2017-09-01 00:00:00‘ AND create_time <‘ 2017-09-28 00:00:00‘ UNION ALL
SELECT SUM(total_fee) AS a FROM order_info7 WHERE create_time >‘ 2017-09-01 00:00:00‘ AND create_time <‘ 2017-09-28 00:00:00‘ UNION ALL
SELECT SUM(total_fee) AS a FROM order_info8 WHERE create_time >‘ 2017-09-01 00:00:00‘ AND create_time <‘ 2017-09-28 00:00:00‘ UNION ALL
SELECT SUM(total_fee) AS a FROM order_info9 WHERE create_time >‘ 2017-09-01 00:00:00‘ AND create_time <‘ 2017-09-28 00:00:00‘
) b;

36.
// 查询当前租户下有几个有操作员
SELECT a.tenant_id,b.enterprise_title,COUNT(*) FROM sysmgnt4paas.`sys_operator` AS a,shop4paas.tenant_info AS b
WHERE a.tenant_id IN (SELECT tenant_id FROM sysmgnt4paas.`sys_role_operator` WHERE state = 1)
AND a.tenant_id = b.tenant_id AND a.state = 1 GROUP BY a.tenant_id ORDER BY COUNT(*) DESC;




















































































































































































































































































以上是关于MYSQL语法总结的主要内容,如果未能解决你的问题,请参考以下文章

MySQL常用语法总结(复习篇)

MySQL常用语法总结(复习篇)

MYSQL语法总结

mysql语法总结

MySQL常用语法总结

mysql 语法总结