首页 / 更多教程 / sql开发技巧总结-2
sql开发技巧总结-2
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了sql开发技巧总结-2,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含3225字,纯文字阅读大概需要5分钟。
内容图文
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 a.user_name;
--序列表 列转行操作
(coalesce不为空)
select user_name
,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 user_name,
case
when c.id = 1 then ‘arms‘
when c.id = 2 then ‘clothig‘
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;
2.如何生成唯一序列号
生成序列号的方法
--(优先选择系统提供的序列号生成方式)
--特殊情况下可以使用SQL方式生成序列号
mysql AUTO_INCREMENT
sql_server IDENTITY/SEQUENCE
Oracle SEQUENCE
需求:生成订单序列号 并且订单号格式如下
YYYYMMDDNNNNNNN 如201505120000003
3.如何删除重复数据
产生数据重复的原因:
人为:重复录入数据 重复提交
系统:由于系统升级或者设计使原来可以重复的数据变为不可用
如何查询数据是否重复
group by having
select user_name,COUNT(*)
FROM user1_test
GROUP BY user_name HAVING COUNT(*)>1
如何删除重复数据 对于相同数据保留ID最大的
更复杂情况
函数:
1、从左开始截取字符串
left(str, length)
说明:left(被截取字段,截取长度)
例:select left(content,200) as abstract from my_content_t
2、从右开始截取字符串
right(str, length)
说明:right(被截取字段,截取长度)
例:select right(content,200) as abstract from my_content_t
3、截取字符串
substring(str, pos)
substring(str, pos, length)
说明:substring(被截取字段,从第几位开始截取)
substring(被截取字段,从第几位开始截取,截取长度)
例:select substring(content,5) as abstract from my_content_t
select substring(content,5,200) as abstract from my_content_t
(注:如果位数是负数 如-5 则是从后倒数位数,到字符串结束或截取的长度)
4、按关键字截取字符串
substring_index(str,delim,count)
说明:substring_index(被截取字段,关键字,关键字出现的次数)
例:select substring_index("blog.jb51.net","。",2) as abstract from my_content_t
结果:blog.jb51
(注:如果关键字出现的次数是负数 如-2 则是从后倒数,到字符串结束)
SUBSTRING(str,pos) , SUBSTRING(str FROM pos) SUBSTRING(str,pos,len) , SUBSTRING(str FROM pos FOR len)
不带有len 参数的格式从字符串str返回一个子字符串,起始于位置 pos。带有len参数的格式从字符串str返回一个长度同len字符相同的子字符串,起始于位置 pos。 使用 FROM的格式为标准 SQL 语法。也可能对pos使用一个负值。假若这样,则子字符串的位置起始于字符串结尾的pos 字符,而不是字符串的开头位置。在以下格式的函数中可以对pos 使用一个负值。
sql开发技巧总结-2
标签:如何 ide server cross 语法 src 并且 删除重复数据 开发技巧
本文系统来源:http://www.cnblogs.com/weizaiyes/p/6986848.html
内容总结
以上是互联网集市为您收集整理的sql开发技巧总结-2全部内容,希望文章能够帮你解决sql开发技巧总结-2所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。