-
@MySQL---用SQL生成特殊的序列号
CREATE TABLE order_seq( timestr INT UNSIGNED, order_sn INT UNSIGNED, );DELIMITER //CREATE PROCEDURE seq_no()BEGINDECLARE v_cnt INT UNSIGNED;DECLARE v_timestr INT UNSIGNED;DECLARE rowcount BIGINT;SET v_timestr = DATE_FORMAT(NOW(),'%Y%m%d');SELECT ROUND(RAND()*100,0)+1 INTO v_cnt;START TRANSACTION;UPDATE order_seq SET order_sn = order_sn + v_cnt WHERE timestr = v_timestr;IF ROW_COUNT() = 0 THENINSERT INTO order_seq(timestr,order_sn) VALUES(v_timestr,v_cnt);END IF;SELECT CONCAT(v_timestr,LPAD(order_sn,7,0))AS order_snFROM order_seq WHERE timestr = v_timestr;COMMIT;END//DELIMITER ;CALL seq_no();
查看全部 -
@MySQL---生成唯一序列号方式
优先使用系统提供的生成序列号方式
MySQL:AUTO_INCREMENT
SQLServer:INENTITY/SEQUENCE
Oracle:SEQUENCE
PgSQL:SEQUENCE查看全部 -
@MySQL---使用序列化实现列转行
SELECT user_name, CASE WHEN c.id=1 THEN 'arms' WHEN c.id=2 THEN 'clothing' WHEN c.id=3 THEN 'shoe' END AS equipment, COALESCE(CASE WHEN c.id=1 THEN arms END, CASE WHEN c.id=2 THEN clothing END, CASE WHEN c.id=3 THEN shoe END) AS eq_name FROM user1 a JOIN user1_equipment b ON a.id=b.user_id CROSS JOIN tb_sequence c WHERE c.id<=3 ORDER BY user_name;
查看全部 -
@MySQL---使用UNION方法实现列转行
SELECT user_name,'arms' AS equipment,arms FROM user1 a JOIN user1_equipment b ON a.id=b.user_id UNION ALL SELECT user_name,'clothing' AS equipment,clothing FROM user1 a JOIN user1_equipment b ON a.id=b.user_id UNION ALL SELECT user_name,'shoe' AS equipment,shoe FROM user1 a JOIN user1_equipment b ON a.id=b.user_id ORDER BY user_name;
查看全部 -
@MySQL---利用序列表转行的数据
SELECT user_name, REPLACE(SUBSTRING(SUBSTRING_INDEX(mobile,',',a.id),CHAR_LENGTH(SUBSTRING_INDEX(mobile,',',a.id-1))+1),',','') AS mobile FROM tb_sequence a CROSS JOIN(SELECT user_name,CONCAT(mobile,',') AS mobile, LENGTH(mobile)-LENGTH(REPLACE(mobile,',',''))+1 size FROM user1 b) b ON a.id<=b.size;
查看全部 -
@MySQL---使用CASE语句实现行列转换
SELECT SUM(CASE WHEN user_name='孙悟空' THEN kills END) AS '孙悟空', SUM(CASE WHEN user_name='猪八戒' THEN kills END) AS '猪八戒', SUM(CASE WHEN user_name='沙僧' THEN kills END) AS '沙僧' FROM user1 a JOIN user_kills b ON a.id=b.user_id;
查看全部 -
@MySQL---使用自连接实现行转列
1.比如成绩
分别查询出不同同学的成绩,并将字段名改为同学的名字。
通过交叉连接,将不同的语句连接起来。SELECT * FROM ( SELECT SUM(kills) AS 'a' FROM a INNER JOIN b ON a.name=b.user_name WHERE a.name='a') AS a; CROSS JOIN( SELECT SUM(kills) AS 'b' FROM a INNER JOIN b ON a.name=b.user_name WHERE a.name='b') AS b; CROSS JOIN( SELECT SUM(kills) AS 'c' FROM a INNER JOIN b ON a.name=b.user_name WHERE a.name='c') AS c;
2.缺点:是将原来查询的结果每一行单独查询出来,再进行拼接。
因此每增加一个同学就增加一个SELECT语句。并且是通过交叉连接,
要保证每个查询的结果只能是一个,不然没办法通过交叉连接实现转换。查看全部 -
行转列
case when
查看全部 -
cross join 行转列
查看全部 -
行转列
我们的例子
查看全部 -
行转列场景2查看全部
-
行转列场景1
查看全部 -
最后的行列转换在oracle中没有实现
查看全部 -
进行数据表中的行行转换,统计表格中的数据查看全部
-
行转列:cross join 利用自身连接来实现: SELECT * FROM ( SELECT SUM(KILLS) AS 'A' FROM A INNER JOIN B ON A.NAME=B.USER_NAME WHERE A.NAME='A') AS A CROSS JOIN( SELECT SUM(KILLS) AS 'B' FROM A INNER JOIN B ON A.NAME=B.USER_NAME WHERE A.NAME='B') AS B CROSS JOIN( SELECT SUM(KILLS) AS 'C' FROM A INNER JOIN B ON A.NAME=B.USER_NAME WHERE A.NAME='C')AS C; 比如成绩 1、分别查询出不同同学的成绩,并将字段名改为同学的名字 2、通过交叉连接,将不同的语句连接起来 缺点:是将原来查询的结果每一行单独查询出来,再进行拼接。因此每增加一个同学就增加一个SELECT语句。并且是通过交叉连接,要保证每个查询的结果只能是一个,不然没办法通过交叉连接实现转换。查看全部
举报
0/150
提交
取消