为了账号安全,请及时绑定邮箱和手机立即绑定
  • 利用group by 和having从句查找重复数据 select user_name,count(*) form user1_test group by user_name having count(*)>1 删除重复数据,保留重复数据中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
    查看全部
  • 使用存储过程生成唯一订单号,每秒中可生成1千个
    查看全部
  • 优先选择系统提供的序列号生成方式 mysql的auto_increment的方式如果使用事务回滚会产生空洞情况
    查看全部
  • 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;
    查看全部
  • 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
    查看全部
  • 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 use_kills b on a.id = b.user_id;
    查看全部
  • 使用序列化方法转换: select user_name, replace(substring(substring_index(mobile,','a.id),char_length(substring_index(mobile,',',a.id-1))+1),',','') as mobile --截取字符串 from tb_sequence as a cross join( select user_name, concat(mobile,',') as mobile, --在mobile结尾增加逗号 length(mobile)-length(replace(mobile,',',''))+1 as size --通过mobile总长度-排除逗号后的长度+1计算得出总段数 from user1 as b ) as b on a.id<=b.size
    查看全部
  • 使用union all连接转换: 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
    查看全部
  • 1.判断数据是否重复: SELECT user_name , over, count(*) FROM user1_test GROUP BY user_name, over HAVING count(*)>1 2.删除重复数据,对于相同数据保留ID最大的 DELETE * 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 将会报错
    查看全部
  • 211·
    查看全部
  • 通过存储过程和事务等确保生成唯一序列号。可以通过创建一张表,记录某天的流水号最大是什么,然后通过某一个数可以叠加起来。 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;
    查看全部
  • 序列化方式进行行转列:<br> 1.首先我们先建立一个序列表<br> 2.思路:<br> 首先我们通过cross join 将user1_equipment 和 序列表 进行笛卡尔积,变成最终有多少条记录 c.id 必须小于等于user1_equipment的列数<br> 再通过case语句,分别取c.id=1,2,3...的对应的行的值,加上coalesce取不为空的数据<br> 最后加上对应的列名,<br> 示例如下:<br> SELECT user_name,<br> CASE WHEN c.id = 1 THEN 'arms'<br> WHEN c.id = 2 THEN 'clothing'<br> WHEN c.id = 3 THEN 'shoe'<br> END AS equipment,<br>//生成装备列 COALESCE(<br> CASE WHEN c.id = 1 THEN arms END<br> CASE WHEN c.id = 2 THEN clothing END<br> CASE WHEN c.id = 3 WHEN shoe END <br> ) AS eq_name<br>//coalesce取不为空的数据,那条不为空取那一条 FROM user1 a<br> JOIN user1_equipment b ON a.id=b.user_id<br> CROSS JOIN tb_sequence c WHERE c.id <= 3 ORDER BY user_name;
    查看全部
  • 实现如图的列转行的方法: 1.使用union all 将结果集 合并 select user_name,'arms' as equipment, arms from user1 a join user1_equipment b on a.id=user_id<br> union all<br> select user_name,'clothing' as equipment, clothing from user1 a join user1_equipment b on a.id=user_id;<br> union all<br> select user_name,'shoe' as equipment, shoe from user1 a join user1_equipment b on a.id=user_id;
    查看全部
  • 列转行另一种场景
    查看全部
  • 列转行技巧: 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,',') as mobile,//concat在字符串后面添上逗号 length(mobile)-length(replace(mobile,',',''))+1 as size //显示出逗号的个数 from user1 as b) as b on a.id<=b.size
    查看全部

举报

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

微信扫码,参与3人拼团

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

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