为了账号安全,请及时绑定邮箱和手机立即绑定
  • 列转行场景
    查看全部
  • 使用cross join 方式是实现行列转换: 1.统计每一个用户打怪的总数 select sum(kills) as '沙僧' from user1 a left join user_kills b on a.id = b.user_id where a.username='沙僧'; select sum(kills) as '猪八戒' from user1 a left join user_kills b on a.id = b.user_id where a.username='猪八戒'; select sum(kills) as '孙悟空' from user1 a left join user_kills b on a.id = b.user_id where a.username='孙悟空'; 2.使用cross join 进行笛卡尔积连接,将三组数据进行行转列组合 select * from (select sum(kills) as '沙僧' from user1 a left join user_j=kills b on a.id = b.user_id and a.username='沙僧') a cross join (select sum(kills) as '猪八戒' from user1 a left join user_j=kills b on a.id = b.user_id and a.username='猪八戒') b cross join (select sum(kills) as '孙悟空' from user1 a left join user_j=kills b on a.id = b.user_id and a.username='孙悟空') c 总结:这种方法虽然可以转换行列,但是遇到需要组合的数据太多编写起来就比较繁琐,而且查询效率比较低!
    查看全部
  • 2.使用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.判断数据是否重复: SELECT user_name , over, count(*) FROM user1_test GROUP BY user_name, over HAVING count(*)>1 2.删除重复数据,对于相同数据保留ID最大的 DELETE a FROM user1_test a JOIN( SELECT user_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 3.将表中查询的数据再次插入到表中 INSERT INTO xmss_wh(XM_ID,WH_ID,YJ,SH,SHR,FJ_NAME,FJ_ID) SELECT XM_ID,WH_ID,YJ,SH,SHR,FJ_NAME,FJ_ID FROM xmss_wh WHERE ID=28; sql中的group by 用法解析: Group By语句从英文的字面意义上理解就是“根据(by)一定的规则进行分组(Group)”。 它的作用是通过一定的规则将一个数据集划分成若干个小的区域,然后针对若干个小区域进行数据处理。 --注意:group by 是先排序后分组; --举例子说明:如果要用到group by 一般用到的就是“每这个字” 例如说明现在有一个这样的表:每个部门有多少人 就要用到分组的技术 select DepartmentID as '部门名称', COUNT(*) as '个数' from BasicDepartment group by DepartmentID 这个就是使用了group by +字段进行了分组,其中我们就可以理解为我们按照了部门的名称ID DepartmentID将数据集进行了分组;然后再进行各个组的统计数据分别有多少; 如果不用count(*)等类似函数 select DepartmentID,DepartmentName from BasicDepartment group by DepartmentID 将会报错
    查看全部
  • 列转行
    查看全部
  • sql行转列的二
    查看全部
  • 行转列的场景一报表统计
    查看全部
  • sql技巧二
    查看全部
  • 删除重复数据 delete a from user1_test a join ( select user_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
    查看全部
  • 列转行2 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; ========================================== 列转行3 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;
    查看全部
  • 列转行技巧: 1.首先我们加一张序列号表,所谓序列号表就是一张有一个自增字段的表,如id 1,2,3,4,5,6...... 2.使用这条sql语句,这句有点整不懂。 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,','))+1 size FROM user1 b) b ON a.id <= b.size
    查看全部
  • 使用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
    查看全部
  • 删除重复
    查看全部
  • 列转行
    查看全部

举报

0/150
提交
取消
课程须知
本门课程主要针对的是MySQL数据库,熟悉数据库的CRUD操作是掌握本门课程精华的必要条件。
老师告诉你能学到什么?
1、如何进行行列转换 2、如何生成唯一序列号 3、如何删除重复数据

微信扫码,参与3人拼团

意见反馈 帮助中心 APP下载
官方微信
友情提示:

您好,此课程属于迁移课程,您已购买该课程,无需重复购买,感谢您对慕课网的支持!