MySql(二)
2017-02-08 16:51
176 查看
Mysql
行列转换
第二种方法采用CASE方式
列转行:
1. 创建序列表
2.
第二种列转行
3.
a156
生成唯一序列号使用自增序列设置序列号
1. 使用sql编写特殊序列号
2.
删除重复数据
1. 判断是否重复
删除重复数据,同时保留id最大的那条记录
较为复杂的重复数据的处理:
相关视频参考慕课网http://www.imooc.com/learn/427
行列转换
Select username,kills from user1 a join user_kills b on a.id = b.id; Select username,SUM(kills) from user1 join user1 a join user_kills b on a.id=b.id; 第一种方法用 cross join: select * from ( select sum(kills) as '孙悟空' from user1 a join user_kills b on a.id = b.use_id and a.user_name='孙悟空' ) a cross join ( select sum(kills) as '猪八戒' from user1 a join user_kills b on a.id = b.use_id and a.user_name='猪八戒' ) b cross join( select sum(kills) as '沙僧' from user1 a join user_kills b on a.id = b.use_id and a.user_name='沙僧' ) c
第二种方法采用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;
列转行:
1. 创建序列表
2.
select user_name,replace(substring(substring_index(mobile,',',a_id),char_length( substring_index(mobile,',',a.id-1),',',")as mobile from tb_sequernce 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;
第二种列转行
3.
select user_name,'arms' as equipment, arms from user1 a join user1_equipment b on a.id=user_id union all select user_name,'clothing' as equipment, clothing from user1 a join user1_equipment b on a.id=user_id; union all select user_name,'shoe' as equipment, shoe from user1 a join user1_equipment b on a.id=user_id;
a156
select username, (case when s.id=1 then 'arms' when s.id=2 then 'clothing' when s.id=3 then 'shoe' end) equipment, (case when s.id=1 then arms when s.id=2 then clothing when s.id=3 then shoe end) eq_name from t_equipment e join t_user u on e.userid = u.userid cross join t_sequence s where s.id<=3 order by username
生成唯一序列号使用自增序列设置序列号
1. 使用sql编写特殊序列号
2.
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; 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;
删除重复数据
1. 判断是否重复
select user_name ,count(*) from test group by user_name having count(*)>1
删除重复数据,同时保留id最大的那条记录
delete a from user1_test a join( select user1_name count(*),max(id) as id from user1_test group by user_name having count(*) >1 )b on a.user_name = b.user_name where a.id<b.id
较为复杂的重复数据的处理:
sql: select username,GROUP_CONCAT(phone) mobile from ( select DISTINCT a.username, REPLACE(SUBSTRING(SUBSTRING_INDEX(a.mobile,',',s.id),LENGTH(SUBSTRING_INDEX(a.mobile,',',s.id-1))+1),',','') phone from t_sequence s join (select username, CONCAT(mobile,',') mobile, (LENGTH(mobile)-LENGTH(REPLACE(mobile,',',''))+1) count from t_user where mobile is not null) a on s.id <= a.count )as b GROUP BY username
相关视频参考慕课网http://www.imooc.com/learn/427
相关文章推荐
- apache+mysql+php+ssl服务器之完全安装攻略
- mysql表锁、行锁、索引之间暧昧的关系
- MySQL丢了root密码怎么办
- 使用Lccwin32进行MySQL开发。
- MySQL索引分析和优化
- 采用ODBC接口访问MySQL指南
- 为何不用MySQL?
- MySQL的转义字符“/”
- PHP 和 MySQL 开发的 8 个技巧
- ASP链接MYSQL并读取数据的程序
- 浅谈PHP+MYSQL身份验证的方法
- MySql实现简单的注册登录(易语言)
- IIS+PHP+MySQL+Zend配置 (视频教程)
- 聊天室php&mysql(二)
- PHP4 与 MySQL 交互使用
- 在php和MySql中计算时间差的方法
- 安装与配置MySQL,phpmyAdmin;安装与测试PHPLIB
- 在php和MySql中计算时间差的方法详解
- PHP+MySQl的事务处理
- 基于MySQL的高性能数据库应用开发