最好加上else ,防止空值
SELECT
sum(CASE WHEN use_name = "沙僧"
THEN b.kills
ELSE 0 END) AS '沙僧',
sum(CASE WHEN use_name = "猪八戒"
THEN b.kills
ELSE 0 END) AS '猪八戒'
FROM im_user AS a
INNER JOIN im_kill AS b ON b.user_id = a.id;
SELECT
sum(CASE WHEN use_name = "沙僧"
THEN b.kills
ELSE 0 END) AS '沙僧',
sum(CASE WHEN use_name = "猪八戒"
THEN b.kills
ELSE 0 END) AS '猪八戒'
FROM im_user AS a
INNER JOIN im_kill AS b ON b.user_id = a.id;
2017-03-19
UPDATE order_seq SET order_sn = order_sn + v_cnt WHERE timestr = v_timestr;
IF ROW_COUNT() = 0 THEN
INSERT INTO order_seq(timestr,order_sn) VALUES(v_timestr,v_cnt);
END IF;
SELECT CONCAT(v_timestr,LPAD(order_sn,7,0))AS order_sn
FROM order_seq WHERE timestr = v_timestr;
COMMIT;
IF ROW_COUNT() = 0 THEN
INSERT INTO order_seq(timestr,order_sn) VALUES(v_timestr,v_cnt);
END IF;
SELECT CONCAT(v_timestr,LPAD(order_sn,7,0))AS order_sn
FROM order_seq WHERE timestr = v_timestr;
COMMIT;
2017-01-07
DECLARE v_cnt INT;
DECLARE v_timestr INT;
DECLARE rowcount BIGINT;
SET v_timestr = DATE_FORMAT(NOW(),'%Y%m%d');
SELECT ROUND(RAND()*100,0)+1 INTO v_cnt;
START TRANSACTION;
DECLARE v_timestr INT;
DECLARE rowcount BIGINT;
SET v_timestr = DATE_FORMAT(NOW(),'%Y%m%d');
SELECT ROUND(RAND()*100,0)+1 INTO v_cnt;
START TRANSACTION;
2017-01-07