---恢复内容开始---

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的更多相关文章

  1. SQL开发技巧(二)

    本系列文章旨在收集在开发过程中遇到的一些常用的SQL语句,然后整理归档,本系列文章基于SQLServer系列,且版本为SQLServer2005及以上-- 文章系列目录 SQL开发技巧(一) SQL开 ...

  2. SQL开发技巧(二) 【转】感觉他写的很好

    本文转自: http://www.cnblogs.com/marvin/p/DevelopSQLSkill_2.html 本系列文章旨在收集在开发过程中遇到的一些常用的SQL语句,然后整理归档,本系列 ...

  3. Sql开发技巧

    原文:Sql开发技巧 简介 本文主要介绍下述几个技巧: 使用Row_Number分页 事务 根据条件刷选记录的技巧 分页 主要是使用了Row_Number()这个函数.一般如下: declare @P ...

  4. sql开发技巧总结-1

    1.数据库分类 关系型 非关系型 2.sql语句分类 sql: ddl数据库定义语言  tpl事物处理语言 dcl数据控制语言  dml数据操作语言(insert delete update sele ...

  5. Mysql - 开发技巧(二)

    本文中的涉及到的表在https://github.com/YangBaohust/my_sql中 本文衔接Mysql - 巧用join来优化sql(https://www.cnblogs.com/dd ...

  6. SQL优化技巧

    我们开发的大部分软件,其基本业务流程都是:采集数据→将数据存储到数据库中→根据业务需求查询相应数据→对数据进行处理→传给前台展示.对整个流程进行分析,可以发现软件大部分的操作时间消耗都花在了数据库相关 ...

  7. DelphiXE2 DataSnap开发技巧收集

    DelphiXE2 DataSnap开发技巧收集 作者:  2012-08-07 09:12:52     分类:Delphi     标签: 作为DelphiXE2 DataSnap开发的私家锦囊, ...

  8. delphi XE5下安卓开发技巧

    delphi XE5下安卓开发技巧 一.手机快捷方式显示中文名称 project->options->Version Info-label(改成需要显示的中文名即可),但是需要安装到安卓手 ...

  9. thinkphp开发技巧经验分享

    thinkphp开发技巧经验分享 www.111cn.net 编辑:flyfox 来源:转载 这里我给大家总结一个朋友学习thinkphp时的一些笔记了,从变量到内置模板引擎及系统变量等等的笔记了,同 ...

随机推荐

  1. java代码继承------多层继承

    总结:继承.方法的重要性, 运行结果显示: class A is callingclass B is callingclass C is calling package com.addd; //jav ...

  2. 四川第七届 D Vertex Cover(二分图最小点覆盖,二分匹配模板)

    Vertex Cover frog has a graph with nn vertices v(1),v(2),…,v(n)v(1),v(2),…,v(n) and mm edges (v(a1), ...

  3. Java-API-Package:org.springframework.web.bind.annotation

    ylbtech-Java-API-Package:org.springframework.web.bind.annotation 1.返回顶部 1. @NonNullApi @NonNullField ...

  4. 【转】js获取对象的所有属性和方法

    //有时候需要知道一个js对像的所有属性和方法来帮助调试,下面是再网上找到的一个比较给力的方法 function displayProp(obj){ var names=""; f ...

  5. 开发环境入门 linux基础(部分)虚拟内存,rpm和yum安装

    虚拟内存,rpm和yum安装 文本中查找 /内容 替换:扩展模式下(:)%s /替换目标/要替换的文件/ (只替换第一个)(后边加g全部替换) :set u添加行号 raid  lvm逻辑卷 df - ...

  6. Javascript ——Navigator对象

    见 <Javascript 高级程序设计 第二版> P172 一.检测插件: 1.获取所有插件名称: 非IE浏览器:根据plugins数组, function getplugins() { ...

  7. Shell杀tomcat进程

    一.killandclean.sh #!/bin/bash pid=($(ps -ef | grep tomcat | egrep -v grep | awk '{print $2}')) lengt ...

  8. CentOS和Ubuntu系统下安装vsftp(助推大数据部署搭建)

    不多说,直接上干货! 同时,声明,我这里安装的vsftp,仅仅只为我的大数据着想,关于网上的复杂安装,那是服务和运维那块.我不多牵扯,也不多赘述. 一.CentOS系统里安装vsftp 第一步:使用y ...

  9. 【259】ucpole.dat update

    2017年2月21日 57871 +0.020896 0.007232 +0.414732 0.009212 +0.418044 0.007533 p 57872 +0.022055 0.007284 ...

  10. day17 14.dao模式介绍

    Web的三层架构,不是MVC,Web层,Service层,DAO层. 之前玩的JSP Servlet JavaBean那是MVC模式,那玩意只是表现层的东西. 转账汇款的例子. 说了这么多有啥用啊,一 ...