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語法總結