1. 程式人生 > >記錄寫過的有代表性SQL

記錄寫過的有代表性SQL

查詢從現在開始向前數3個月的訂單	
SELECT * FROM f_order WHERE DATE_FORMAT( createtime, '%Y-%m-%d' ) > DATE_FORMAT( DATE_ADD( now( ), INTERVAL - 3 MONTH ), '%Y-%m-%d' );

查詢向前數90天的所有資料
SELECT * FROM f_order WHERE to_days(now()) - to_days(createtime) <= 90;

查詢訂單某些訂單具體時間
SELECT createtime,date(createtime),TIME(createtime),DAY(createtime),YEAR(createtime) FROM f_order;

sql語句if用法
SELECT if(amount>10,amount/10,amount) AS amount FROM f_order WHERE uid = 1456;

同步修改資料
UPDATE f_order f INNER JOIN (SELECT f.oid,f.uid,u.cid cid FROM f_order f LEFT JOIN f_user u ON f.uid = u.uid) e
 SET f.ecid = e.cid
 WHERE f.oid=e.oid;

插入查詢資料
INSERT INTO f_order_settlement ( sn, total_amount, ele_amount, service_amount ) 
SELECT sn,( ele_price + server_price ) total_amount,ele_price,server_price FROM f_order WHERE ostate = 1;

修改資料
UPDATE e_account_info f SET f.cid = 10,phone = '15928528345' WHERE uid = 379;

 

備份一條資料

-- 先刪除企業的測試賬戶
DELETE FROM f_user_company where cid in (181,267,353,405,443,447,512,524,582,655,695,696,946,419,508,515,516,671,59,85,245,254,527);
DELETE FROM f_order where uid in (181,267,353,405,443,447,512,524,582,655,695,696,946,419,508,515,516,671,59,85,245,254,527);
DELETE FROM f_account_info where uid in (181,267,353,405,443,447,512,524,582,655,695,696,946,419,508,515,516,671,59,85,245,254,527);
DELETE FROM e_account_info where uid in (181,267,353,405,443,447,512,524,582,655,695,696,946,419,508,515,516,671,59,85,245,254,527);
DELETE FROM f_user where phone in (13540046686,15881002317,18482180351,17323170819,18380111778,18380438130,13378168066,18200114595,15828479653,15608002876,15202822089,18011283744,18700244990,18382899620,18382899620,13348903857,18011283744,18281569315,18908069158,18215530873,15608283273,15108396721,18381005946,18408249234,15228119181);

DROP TABLE IF EXISTS `f_monitor_order_temp`;
CREATE TABLE `f_monitor_order_temp` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `uid` int(11) DEFAULT NULL COMMENT '使用者id',
  `oid` int(11) DEFAULT NULL COMMENT '訂單id',
  `ele_price` decimal(22,2) DEFAULT '0.00' COMMENT '計算後電費',
  `server_price` decimal(22,2) DEFAULT '0.00' COMMENT '計算後服務費',
  `useele` double(10,2) DEFAULT '0.00' COMMENT '計算後用電量',
  `bef_useele` double(10,2) DEFAULT '0.00' COMMENT '原始用電量',
  `bef_eleprice` decimal(22,2) DEFAULT '0.00' COMMENT '原始電費',
  `bef_serverprice` decimal(22,2) DEFAULT '0.00' COMMENT '原始服務費',
  `server_danjia` decimal(22,1) DEFAULT '0.0' COMMENT '服務費單價',
  `pileunm` varchar(100) DEFAULT NULL COMMENT '樁號',
  `sname` varchar(255) DEFAULT NULL COMMENT '電站名',
  `remark` varchar(255) DEFAULT NULL COMMENT '修正原因',
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=4450 DEFAULT CHARSET=utf8;

-- 建立表
DROP TABLE IF EXISTS `f_monitor_e_order_temp`;
CREATE TABLE `f_monitor_e_order_temp` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `uid` int(11) DEFAULT NULL COMMENT '使用者id',
  `cid` int(11) DEFAULT NULL COMMENT '企業id',
  `phone` varchar(50) DEFAULT NULL COMMENT '電話',
  `e_recharge` decimal(20,2) NOT NULL DEFAULT '0.00' COMMENT '企業子賬號總充值',
  `e_balance` decimal(20,2) NOT NULL DEFAULT '0.00' COMMENT '企業子賬號剩餘金額',
  `f_consume` decimal(22,2) NOT NULL DEFAULT '0.00' COMMENT '訂單表消費金額',
  `c_type` varchar(20) DEFAULT NULL COMMENT '消費正常,消費異常',
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=393 DEFAULT CHARSET=utf8;


-- 建立表, 複製f_order中的資料到 f_repair_e_order_history_20180722表中
DROP TABLE IF EXISTS `f_repair_e_order_history_20180722`;
CREATE TABLE `f_repair_e_order_history_20180722` (
  `oid` int(11) NOT NULL AUTO_INCREMENT,
  `sn` varchar(100) DEFAULT NULL COMMENT '訂單號',
  `otype` int(11) DEFAULT NULL COMMENT '1-充電訂單 2-預約訂單',
  `ostate` int(11) DEFAULT '1' COMMENT '-1訂單失效 0-正在充電 1-充電結束2準備充電',
  `uid` int(11) DEFAULT NULL COMMENT '使用者uid',
  `amount` decimal(10,2) DEFAULT NULL COMMENT '預付費',
  `couponbalance` decimal(10,2) DEFAULT NULL,
  `realbalance` decimal(10,2) DEFAULT NULL,
  `cid` int(11) DEFAULT NULL COMMENT '如有使用優惠券該大於0 或者不為null ',
  `pid` varchar(50) DEFAULT NULL COMMENT '樁編號',
  `gunnumber` varchar(50) DEFAULT NULL COMMENT '槍號',
  `gid` int(11) DEFAULT NULL COMMENT '槍編號',
  `starttime` datetime DEFAULT NULL COMMENT '訂單開始時間',
  `endtime` datetime DEFAULT NULL COMMENT '訂單結束時間',
  `bsoc` int(11) NOT NULL DEFAULT '0' COMMENT '車輛開始充電時電量',
  `esoc` int(11) NOT NULL DEFAULT '0' COMMENT '車輛結束充電時電量',
  `useele` double(10,2) NOT NULL DEFAULT '0.00' COMMENT '使用電量',
  `ele_price` decimal(10,2) NOT NULL DEFAULT '0.00' COMMENT '電價',
  `end_reason` int(11) NOT NULL DEFAULT '0' COMMENT '結束原因(棄用)',
  `use_type` int(11) NOT NULL DEFAULT '0' COMMENT '啟動型別(棄用)',
  `scro` int(11) NOT NULL DEFAULT '0' COMMENT '贈送積分',
  `car_id` varchar(50) DEFAULT NULL COMMENT '車牌',
  `car_vin` varchar(50) DEFAULT NULL COMMENT '車輛唯一碼',
  `server_price` decimal(10,2) NOT NULL DEFAULT '0.00' COMMENT '服務費',
  `createtime` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '建立時間',
  `roid` int(11) DEFAULT NULL COMMENT '預約產生一個訂單,到達現場掃碼支付也產生訂單,該欄位用於關聯到之前的預約訂單.',
  `paytype` int(11) DEFAULT NULL COMMENT '0-app餘額支付 1-電卡支付 2企業賬戶',
  `order_time` varchar(50) DEFAULT NULL COMMENT '預約時間(預約訂單用)',
  `is_push` int(11) NOT NULL DEFAULT '0',
  `is_comment` int(11) NOT NULL DEFAULT '0',
  `back_money` decimal(10,2) NOT NULL DEFAULT '0.00' COMMENT '預付費退款金額',
  `card_sn` varchar(50) NOT NULL DEFAULT '0.00',
  `bef_eleprice` decimal(22,2) DEFAULT NULL COMMENT '原始電費',
  `bef_server_price` decimal(22,2) DEFAULT NULL COMMENT '原始服務費',
  `bef_useele` decimal(22,2) DEFAULT NULL COMMENT '原始用電量',
  `ele_price_repair` decimal(22,2) DEFAULT NULL COMMENT '修正電費',
  `server_price_repair` decimal(22,2) DEFAULT NULL COMMENT '修正服務費',
  `server_danjia` decimal(22,1) DEFAULT '0.0' COMMENT '服務費單價',
  `repair_createtime` datetime DEFAULT NULL COMMENT '修正時間',
  `repair_remarke` varchar(255) DEFAULT NULL COMMENT '修正原因',
  `repair_user` varchar(50) DEFAULT NULL COMMENT '修正人',
  PRIMARY KEY (`oid`) USING BTREE
) ENGINE=InnoDB AUTO_INCREMENT=20178 DEFAULT CHARSET=utf8 COMMENT='充電訂單';

-- 清空表
TRUNCATE table f_repair_e_order_history_20180722;

-- 複製資料
INSERT INTO f_repair_e_order_history_20180722(oid,sn,otype,ostate,uid,amount,couponbalance,realbalance,cid,pid,gunnumber,gid,starttime,endtime,bsoc,esoc,useele,ele_price,end_reason,use_type,scro,car_id,car_vin,server_price,createtime,roid,paytype,order_time,is_push,is_comment,back_money,card_sn)
SELECT oid,sn,otype,ostate,uid,amount,couponbalance,realbalance,cid,pid,gunnumber,gid,starttime,endtime,bsoc,esoc,useele,ele_price,end_reason,use_type,scro,car_id,car_vin,server_price,createtime,roid,paytype,order_time,is_push,is_comment,back_money,card_sn
FROM f_order 
WHERE ostate = 1 and paytype = 2 and uid not in (92,1064);


-- 為企業充值記錄表, 新增uid
-- SELECT uid,phone FROM f_user where phone in (SELECT phone FROM e_account_info WHERE uid is null);
UPDATE e_account_info e INNER JOIN 
(
SELECT uid,phone FROM f_user
) u
SET e.uid = u.uid
where e.phone = u.phone;


-- 插入所有的企業子賬號 uid, phone
INSERT INTO f_monitor_e_order_temp (uid,phone,cid)
SELECT uid,phone,cid FROM f_user where cid is not null;


-- 更新企業子賬戶充值記錄 - 子賬號剩餘金額  == 訂單表消費金額
UPDATE f_monitor_e_order_temp e INNER JOIN
(
	SELECT uid,SUM(cmoney) cmoney
	FROM e_account_info
	WHERE etype is not null 
	GROUP BY uid
) u
SET e.e_recharge = u.cmoney
WHERE e.uid = u.uid;

UPDATE f_monitor_e_order_temp e INNER JOIN
(
	SELECT uid,amount FROM f_account
) u
SET e.e_balance = u.amount
WHERE e.uid = u.uid;

UPDATE f_monitor_e_order_temp e INNER JOIN
(
	SELECT uid,SUM(ele_price+server_price) a
	FROM f_repair_e_order_history_20180722
	WHERE paytype = 2 
	and ostate = 1 
	GROUP BY uid
) u
SET e.f_consume = u.a
WHERE e.uid = u.uid;

-- 插入所有的企業子賬號 uid, phone
INSERT INTO f_monitor_e_order_temp (uid,phone,cid)
SELECT uid,phone,cid FROM f_user where cid is not null;


-- 更新企業子賬戶充值記錄 - 子賬號剩餘金額  == 訂單表消費金額
UPDATE f_monitor_e_order_temp e INNER JOIN
(
	SELECT uid,SUM(cmoney) cmoney
	FROM e_account_info
	WHERE etype is not null 
	GROUP BY uid
) u
SET e.e_recharge = u.cmoney
WHERE e.uid = u.uid;

UPDATE f_monitor_e_order_temp e INNER JOIN
(
	SELECT uid,amount FROM f_account
) u
SET e.e_balance = u.amount
WHERE e.uid = u.uid;

UPDATE f_monitor_e_order_temp e INNER JOIN
(
	SELECT uid,SUM(ele_price+server_price) a
	FROM f_repair_e_order_history_20180722
	WHERE paytype = 2 
	and ostate = 1 
	GROUP BY uid
) u
SET e.f_consume = u.a
WHERE e.uid = u.uid;


-- 插入所有資料
INSERT INTO f_monitor_order_temp (uid,oid,bef_useele,bef_eleprice,bef_serverprice,server_danjia,pileunm,sname)
SELECT p.uid,p.oid,p.useele,p.ele_price,p.server_price,t.serviceprice,p.pilenum,p.sname FROM f_pile_time_price_temp t RIGHT JOIN (SELECT s.sname,s.ptid,s.address,s.createtime stime,p.* FROM f_pile_site s RIGHT JOIN (SELECT p.sid,p.pname,p.pilenum,p.model,p.manufacture,f.uid,f.amount,f.starttime,f.endtime,f.bsoc,f.esoc,f.useele,f.ele_price,f.server_price,f.createtime,f.oid FROM f_repair_e_order_history_20180722 f LEFT JOIN f_pile p on p.pilenum = f.pid WHERE f.paytype = 2 AND f.ostate = 1 AND f.uid NOT IN ( 92, 1064 )) p on s.sid = p.sid) p on t.id = p.ptid ;


-- 查詢消費異常的企業子賬戶
UPDATE f_monitor_e_order_temp SET c_type = '消費正常' where e_recharge-e_balance = f_consume;

-- SELECT p.uid,p.oid,p.useele,p.ele_price,p.server_price,t.serviceprice,p.pilenum,p.sname FROM f_pile_time_price_temp t RIGHT JOIN (SELECT s.sname,s.ptid,s.address,s.createtime stime,p.* FROM f_pile_site s RIGHT JOIN (SELECT p.sid,p.pname,p.pilenum,p.model,p.manufacture,f.uid,f.amount,f.starttime,f.endtime,f.bsoc,f.esoc,f.useele,f.ele_price,f.server_price,f.createtime,f.oid FROM f_repair_e_order_history_20180722 f LEFT JOIN f_pile p on p.pilenum = f.pid WHERE f.paytype = 2 AND f.ostate = 1 AND f.uid NOT IN ( 92, 1064 ) AND f.useele>0 and ele_price!=0) p on s.sid = p.sid) p on t.id = p.ptid where p.server_price =0 and ele_price >210 and oid = 14308 and  p.sname='成都國際機場T2停車場充電站'   ;
-- 

-- 有電量,無服務費,596
-- 1.有電量,無服務費, 服務費=電量/100 * 服務費單價,電費= 之前的電費-計算之後的服務費
UPDATE f_monitor_order_temp 
SET  ele_price = bef_eleprice - ((bef_useele/100) * server_danjia),
	server_price = bef_useele/100 * server_danjia,
	useele = bef_useele,
	remark = '有電量,無服務費, 服務費=電量/100 * 服務費單價,電費= 之前的電費-計算之後的服務費'
WHERE bef_eleprice <> 0 and bef_serverprice = 0 and bef_useele > 0;

-- 有電量,無服務費, 服務費 = 電費 * 單價,電費= 之前的電費-計算後的服務費  
UPDATE f_monitor_order_temp 
SET  server_price = bef_eleprice * server_danjia,
	ele_price = bef_eleprice - bef_eleprice * server_danjia,
	useele = bef_useele,
	remark = '有電量,無服務費, 服務費 = 電費 * 單價,電費= 之前的電費-計算後的服務費'
WHERE bef_eleprice <> 0 and bef_serverprice = 0 and bef_useele > 0 and ele_price < 0;
-- 
-- select * from f_monitor_order_temp 
-- WHERE bef_eleprice <> 0 and bef_serverprice = 0 and bef_useele > 0 and ele_price<0;

-- 2.有電量,有服務費,並且當前電費>210
-- 服務費 = 電量*服務單價 , 電費 = 當前電費/100-電量*服務單價
UPDATE f_monitor_order_temp 
SET server_price = bef_useele/100 * server_danjia,
		ele_price = bef_eleprice/100 - (bef_useele/100 * server_danjia),
		useele = bef_useele,
		remark = '服務費 = 電量*服務單價 , 電費 = 當前電費/100-電量*服務單價'
WHERE bef_useele > 0 and bef_serverprice>0 and bef_eleprice>210;

-- select * from f_monitor_order_temp 
-- WHERE bef_useele > 0 and bef_serverprice>0 and bef_eleprice>210;

-- 3.無電量,無服務費,有電費,場站為:成都國際機場T2停車場充電站
-- 用電量 = 當前電費/1.2 , 電費 = 當前電費/1.2 , 服務費 = 當前電費-(當前電費/1.2) 
UPDATE f_monitor_order_temp 
SET server_price = bef_eleprice - (bef_eleprice / 1.2),
		ele_price = bef_eleprice / 1.2,
		useele = bef_eleprice*100/ 1.2,
		remark = '用電量 = 當前電量/1.2 , 電費 = 當前電量/1.2 , 服務費 = 當前電費-(當前電費/1.2)'
WHERE bef_useele = 0 and bef_serverprice=0 and bef_eleprice>0 and sname = '成都國際機場T2停車場充電站';


-- select * from f_monitor_order_temp 
-- WHERE bef_useele = 0 and bef_serverprice=0 and bef_eleprice>0 and sname = '成都國際機場T2停車場充電站';

-- 4.無電量,有服務費,當電費>210,場站為:成都國際機場T2停車場充電站
-- 用電量 = 當前電費/100/1.2 , 電費 = 當前電費/100/1.2 , 服務費 = 當前電費-(當前電費/1.2) 
UPDATE f_monitor_order_temp 
SET server_price = (bef_eleprice/100) - (bef_eleprice/100/ 1.2),
		ele_price = bef_eleprice/100/ 1.2,
		useele = bef_eleprice/ 1.2,
		remark = '用電量 = 當前電量/1.2 , 電費 = 當前電量/100/1.2 , 服務費 = (當前電費/100)-(當前電費/100/1.2)'
WHERE bef_useele = 0 and bef_serverprice>0 and bef_eleprice>210 and sname = '成都國際機場T2停車場充電站';

-- select * from f_monitor_order_temp 
-- WHERE bef_useele = 0 and bef_serverprice>0 and bef_eleprice>210 and sname != '成都國際機場T2停車場充電站';


-- 沒有修改的資料, 更新電費, 電價, 用電量
UPDATE f_monitor_order_temp SET
ele_price = bef_eleprice,
server_price = bef_serverprice,
useele = bef_useele,
remark = '資料正常,未修改'
where remark is null;


-- 根據  f_monitor_order_temp  的oid更新資料庫f_order
UPDATE f_repair_e_order_history_20180722 e INNER JOIN
(
	SELECT * FROM f_monitor_order_temp
) u
SET 
e.useele = u.useele,
e.ele_price = u.ele_price,
e.server_price = u.server_price,
e.bef_eleprice = u.bef_eleprice,
e.bef_server_price = u.bef_serverprice,
e.bef_useele = u.bef_useele,
e.ele_price_repair = u.ele_price,
e.server_price_repair = u.server_price,
e.server_danjia = u.server_danjia,
e.repair_createtime = NOW(),
e.repair_remarke = u.remark,
e.repair_user = 'wangqinmin'
WHERE e.oid = u.oid;


UPDATE f_repair_e_order_history_20180722 SET bef_eleprice = ele_price,ele_price =1.73,bef_server_price=server_price,bef_useele = useele,ele_price_repair = ele_price,server_price_repair = server_price,repair_createtime=now(),repair_remarke='手動修改',repair_user='wangqinmin' where oid = 9631;
UPDATE f_repair_e_order_history_20180722 SET bef_eleprice = ele_price,ele_price = 1.00,bef_server_price=server_price,server_price = 0.03,bef_useele = useele,ele_price_repair = ele_price,server_price_repair = '0.03',repair_createtime=now(),repair_remarke='手動修改',repair_user='wangqinmin' where oid = 9687;


-- 更新f_order表中的企業電費+ 服務費拆分
update f_order f INNER JOIN 
(
	SELECT * FROM f_repair_e_order_history_20180722
) h
SET	
	f.useele = h.useele,
	f.ele_price = h.ele_price,
	f.server_price = h.server_price
WHERE 
	f.oid = h.oid;
	


COMMIT;




-- 廣西小兔出行/中交信達謙和  太異常(全是廣西的2個樁), 只能手動處理
-- uid 為  92, 1064
-- 143條
-- SELECT * FROM f_repair_e_order_history_20180722 where uid in (1064) and paytype = 2 and ostate = 1;
-- SELECT * FROM f_user where uid in (92,1064);
-- -- 18027168060   陳無花  
-- -- 18154590605  廣西
-- 
-- SELECT * FROM f_repair_e_order_history_20180722 where paytype = 2 and ostate = 1 and uid = 1064 and (ele_price >0 or server_price<0);
-- 
-- SELECT * FROM f_repair_e_order_history_20180722 where uid in ( SELECT uid FROM f_user where cid = 25);
-- 
-- SELECT * FROM f_monitor_order_temp where remark is null;
-- 
-- 
-- SELECT * FROM f_user_company;
-- SELECT * FROM e_account_info where uid = 1064;