sql开发技巧总结-2
---恢复内容开始---
1.如何进行行列转换
需求:
列转换成行
select a.`user_name`,sum(b.kills) from user1 a join user_kills b
on a.id = b.user_id group by a.user_name;
-行转换成列
select sum(case when user_name='wukong' then kills end) as 'wukong',
sum(case when user_name='zhubajie' then kills end) as 'zhubajie',
sum(case when user_name='shaseng' then kills end) as 'shaseng'
from user1 a join user_kills b on a.id=b.user_id;
-列转换成行
alter table user1 add column mobile varchar(100);
select user_name,concat(mobile,',') as mobile,LENGTH(mobile)-LENGTH(REPLACE(mobile,',',''))+1 size from contact b; //计算长度
总的:
--union all 列转行操作
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的更多相关文章
- SQL开发技巧(二)
本系列文章旨在收集在开发过程中遇到的一些常用的SQL语句,然后整理归档,本系列文章基于SQLServer系列,且版本为SQLServer2005及以上-- 文章系列目录 SQL开发技巧(一) SQL开 ...
- SQL开发技巧(二) 【转】感觉他写的很好
本文转自: http://www.cnblogs.com/marvin/p/DevelopSQLSkill_2.html 本系列文章旨在收集在开发过程中遇到的一些常用的SQL语句,然后整理归档,本系列 ...
- Sql开发技巧
原文:Sql开发技巧 简介 本文主要介绍下述几个技巧: 使用Row_Number分页 事务 根据条件刷选记录的技巧 分页 主要是使用了Row_Number()这个函数.一般如下: declare @P ...
- sql开发技巧总结-1
1.数据库分类 关系型 非关系型 2.sql语句分类 sql: ddl数据库定义语言 tpl事物处理语言 dcl数据控制语言 dml数据操作语言(insert delete update sele ...
- Mysql - 开发技巧(二)
本文中的涉及到的表在https://github.com/YangBaohust/my_sql中 本文衔接Mysql - 巧用join来优化sql(https://www.cnblogs.com/dd ...
- SQL优化技巧
我们开发的大部分软件,其基本业务流程都是:采集数据→将数据存储到数据库中→根据业务需求查询相应数据→对数据进行处理→传给前台展示.对整个流程进行分析,可以发现软件大部分的操作时间消耗都花在了数据库相关 ...
- DelphiXE2 DataSnap开发技巧收集
DelphiXE2 DataSnap开发技巧收集 作者: 2012-08-07 09:12:52 分类:Delphi 标签: 作为DelphiXE2 DataSnap开发的私家锦囊, ...
- delphi XE5下安卓开发技巧
delphi XE5下安卓开发技巧 一.手机快捷方式显示中文名称 project->options->Version Info-label(改成需要显示的中文名即可),但是需要安装到安卓手 ...
- thinkphp开发技巧经验分享
thinkphp开发技巧经验分享 www.111cn.net 编辑:flyfox 来源:转载 这里我给大家总结一个朋友学习thinkphp时的一些笔记了,从变量到内置模板引擎及系统变量等等的笔记了,同 ...
随机推荐
- Redis codis 搭建测试
codis Codis 是一个分布式 Redis 解决方案, 对于上层的应用来说, 连接到 Codis Proxy 和连接原生的 Redis Server 没有明显的区别,有部分命令支持 Codis ...
- HTML5的离线应用
参考:有趣的HTML5:离线存储——segmentfault HTML5的离线存储 简介 HTML5提供了很多新的功能以及相应的接口,离线存储就是其中的一个.通过浏览器访问Web App需要联网发送请 ...
- linux设置支持中文
LANG="zh_CN.UTF-8" #LANG="zh_CN.GB18030" #LANG=en_US.UTF-8 LANGUAGE="zh_CN. ...
- docker 安装mysql 使用navicat访问 解决
1. 下载Mysql的Docker镜像: 2. 运行镜像,设置root账号初始密码(123456),映射本地宿主机端口3306到Docker端口3306.测试过程没有挂载本地数据盘: 3. 查看已运行 ...
- python_class21
#!/usr/bin/env python # -*- coding: utf-8 -*- """ @version: 3.5 @author: morgana @lic ...
- Unencapsulate SVM root mirror (ZT)
http://www.seedsofgenius.net/solaris/quick-reference-unencapsulate-svm-root-mirror Often times admin ...
- HotSpotVM创建过程(JNI_CreateJavaVM)详解
来自:<Java Performance>第3章 JVM Overview The HotSpot VM's implementation of the JNI_CreateJavaVM ...
- linux命令-sudo普通用户拥有root权限
普通用户权限不够 [root@wangshaojun ~]# su - dennywang[dennywang@wangshaojun ~]$ ls /root/ls: 无法打开目录/root/: 权 ...
- python之简单的函数介绍(http://docs.python.org/3/library)
Python不但能非常灵活地定义函数,而且本身内置了很多有用的函数,可以直接调用. 在上面的网站上我们可以进行查询,Python具体都有哪些函数. 我们也可以再交互命令行中来查找函数: >> ...
- 使用HttpWebRequest POST 文件,带参数
public string HttpUploadFile(string url, string file, string paramName, string contentType, NameValu ...