02. SQL表达式的灵活使用
什么是SQL表达式?在SQL语句中,表达式可以是函数,也可以是列和列之间的混合运算。
很多时候,对于表达式的使用,可以比单独操作表上的列,带来更多方便。
一. 在HAVING中使用表达式
--drop table t
create table t(c1 int,c2 int) insert into t
select 1,100 union all
select 1,200 union all
select 2,100 union all
select 2,200 union all
select 2,300 union all
select 3,50 union all
select 3,200 union all
select 4,50 union all
select 4,200 union all
select 4,300
返回c1,满足:有3个且都大于等于100 的c2 (学校的考试题中很多见)。
select c1 from t
group by c1
having min(c2)>=100 and count(1)=3
同样,表达式也可以用于group by 子句。
二. 在ORDER BY中使用表达式
--drop table t_orderby
create table t_orderby
(
c1 int null,
c2 varchar(10) null,
c3 varchar(10) null
) insert into t_orderby
select 1,'','a1' union all
select 1,'','a2' union all
select 3,'','ab' union all
select 1,'','b1'
1. c2列的数据按'4','1','2'的指定顺序排序
(1) 使用union
select * from t_orderby
where c2=''
union all
select * from t_orderby
where c2=''
union all
select * from t_orderby
where c2=''
(2) 使用表达式方法1
select * from t_orderby
order by charindex(c2,'4,1,2')
(3) 使用表达式方法2,再加个按照c1倒序
select * from t_orderby
order by case
when c2='' then 1
when c2='' then 2
when c2='' then 3
end,c1 desc
2. 随机排序
(1) 要求c2='4'排第一行,其他的行随机排序
select * from t_orderby
order by case
when c2='' then 1
else 1+rand()
end
(2) 所有行随机排序
select * from t_orderby
order by newid()
(3) 随机取出第一行
select top 1 * from t_orderby
order by newid()
3. 要求列c3中数据,先按第一个字符排序,再按第二个字符排序
select * from t_orderby
order by left(c3,1),ASCII(substring(c3,2,1))
三. 在COUNT中使用表达式
--drop table t_count
create table t_count
(
c1 varchar(10) null,
c2 varchar(10) null
) insert into t_count values(null,null)
insert into t_count values('a','b')
insert into t_count values('a','b')
insert into t_count values('c','d')
1. 使用常量表达式避免忽略NULL值
select COUNT(c1) from t_count --
select COUNT(distinct c1) from t_count --
聚合函数中, SUM/AVG/COUNT中的NULL会被忽略,比如:这里的count(c1)忽略了null
select COUNT(*) from t_count --
select COUNT(1) from t_count --
select COUNT(1000) from t_count --
用count(*)不会忽略NULL,同样用count(1)也不会忽略NULL,这里的1就是一个常量表达式,换成其他常量表达式也可以,比如count(1000)。
另外,count(1)和order by 1,2那里的数字意思不一样,order by后面的序号表示列号。
2. 小心表达式值为NULL被忽略
--正常
select count(*) from (select c1,c2 from t_count group by c1,c2) t --
select count(*) from (select distinct c1,c2 from t_count) t --
--有NULL参与了运算,所以表达式值为NULL
select count(distinct c1+c2) from t_count --
四. 在JOIN中使用表达式
--drop table t1,t2
create table t1
(
url varchar(1000)
) create table t2
(
code varchar(1000)
) --insert
insert into t1
select 'http://www.baidu.com/test1' union all
select 'http://www.baidu.com/test2' union all
select 'http://www.baidu.com/test3' union all
select 'www.baidu.com/test1' union all
select 'www.baidu.com/test2' union all
select 'http://www.google.com/test1' union all
select 'http://www.google.com/test2' union all
select 'http://www.sogou.com/test3' union all
select 'http://www.sogou.com/test4' insert into t2
select 'baidu.com' union all
select 'sogou.com'
要求t1,t2表的两个列之间做匹配,t2的列值包含在t1的列值里。
select t2.code,t1.url from t1
inner join t2
on CHARINDEX(t2.code,t1.url) > 0 --结果如下
/*
baidu.com http://www.baidu.com/test1
baidu.com http://www.baidu.com/test2
baidu.com http://www.baidu.com/test3
baidu.com www.baidu.com/test1
baidu.com www.baidu.com/test2
sogou.com http://www.sogou.com/test3
sogou.com http://www.sogou.com/test4
*/
CHARINDEX是做硬匹配,如果是要模糊匹配,可以使用like或者patindex,通配符可以存在表中字段里,也可以在SQL语句中添加,注意like不加通配符的效果和等于(=)一样
--drop table t1,t2
create table t1
(
url varchar(1000)
) create table t2
(
code varchar(1000)
) --insert
insert into t1
select 'baidu%' union all
select 'baidu' union all --CANNOT be found without wildcard like %
select 'baidu.com' union all --identical string can also be found
select 'XXXbaidu.comXXX' union all --CANNOT be found without wildcard like %
select 'sogou%' insert into t2
select 'baidu.com' union all
select 'sogou.com' --like
select t2.code,t1.url
from t1
inner join t2
on t2.code like t1.url
--on t2.code like t1.url+'%'
/*
baidu.com baidu%
baidu.com baidu.com
sogou.com sogou%
*/ --patindex
select t2.code,t1.url
from t1
inner join t2
on patindex(t1.url,t2.code)>0
/*
baidu.com baidu%
baidu.com baidu.com
sogou.com sogou%
*/
事实上,在join或者where条件中,只要能构造出比较运算表达式(返回boolean值),就可以用作判断条件。
02. SQL表达式的灵活使用的更多相关文章
- Azure SQL 数据库的灵活缩放预览版简介
Eron Kelly SQL Server 产品管理部门产品市场营销总经理 几天前,我们宣布了发布 Azure SQL 数据库的灵活缩放公共预览版.新增的灵活缩放功能通过简化开发和管理,简化了扩展和缩 ...
- SQLAlchemy 学习笔记(一):Engine 与 SQL 表达式语言
个人笔记,如有错误烦请指正. SQLAlchemy 是一个用 Python 实现的 ORM (Object Relational Mapping)框架,它由多个组件构成,这些组件可以单独使用,也能独立 ...
- mybatis 动态sql表达式相关应用
一.mybatis 表达式简介 对于mybatis3 ,提供了一种动态sql的方式.通过动态sql我们可以直接在mybatis 的xm映射文件中直接通过条件判断的方式进行查询添加的拼接.mybatis ...
- 【JEECG技术文档】数据权限自定义SQL表达式用法说明
功能介绍 数据规则通过配置自定义sql来实现数据权限的控制,自定义SQL支持表达式取值 其中自定义sql 条件中字段的名称和数据库表的字段名保持一致. 角色授权 用户角色授权,权限测试不要用adm ...
- 使用MyBatis的动态SQL表达式时遇到的“坑”(integer)
现有一项目,ORM框架使用的MyBatis,在进行列表查询时,选择一状态(值为0)通过动态SQL拼接其中条件但无法返回正常的查询结果,随后进行排查. POJO private Integer stat ...
- JDK8新特性02 Lambda表达式02_Lambda语法规则
//函数式接口:只有一个抽象方法的接口称为函数式接口. 可以使用注解 @FunctionalInterface 修饰 @FunctionalInterface public interface MyF ...
- 02 SQL 执行
sql 被保存在 share pool 后, 开始解析, 解析包括语句的语法, 检验及对象, 以确认该用于是否有该对象的权限, 如果这些都通过了, 接下来就要看这个语句之前是否被执行过, 如果是, o ...
- C#3.0新增功能09 LINQ 标准查询运算符 02 查询表达式语法
连载目录 [已更新最新开发文章,点击查看详细] 某些使用更频繁的标准查询运算符具有专用的 C# 语言关键字语法,使用这些语法可以在查询表达式中调用这些运算符. 查询表达式是比基于方法的等效项更具 ...
- javaScript基础-02 javascript表达式和运算符
一.原始表达式 原始表达式是表达式的最小单位,不再包含其他表达式,包含常量,直接量,关键字和变量. 二.对象和数组的初始化表达式 对象和数组初始化表达式实际上是一个新创建的对象和数组. 三.函数表达式 ...
随机推荐
- c++中指针类型在c#中怎么对应?
int[] a=new int[5]; //取a[3]的地址 IntPtr addr=System.Runtime.InteropServices.Marshal.UnsafeAddrOfPinned ...
- Spring中的设计模式
[Spring中的设计模式] http://www.uml.org.cn/j2ee/201301074.asp [详解设计模式在Spring中的应用] [http://www.geek521.c ...
- Spring MVC体系结构
[Spring MVC类图]<Spring实战>中:<Spring3.0就这么简单>中:[http://blog.csdn.net/gstormspire/article/de ...
- Form时间交叉验证唯一性
*================================================== ** PROCEDURE: check_effcitive_utl Descr ...
- myeclipse、eclipse中项目复制后(可能无法访问)注意事项 .
.myEclipse 复制后修改名称,访问不到项目 这是因为,你只是改了项目的名称,而没有改 下面是解决方法: 方法 1.右击你的项目,选择“properties”,在“type filter tex ...
- CentOS6.5下安装MariaDB5.5.36
yum groupinstall -y "Development Tools" yum install -y cmake openssl-devel zlib-devel yum ...
- python flask model 序列化
class DictSerializable(object): def as_dict(self,*args): result = OrderedDict() ...
- 推荐资料——最受网友力荐的30份HTML前端开发资料
w3cmark最近会新增一个栏目,专注于搜集前端资源的下载,包括和前端相关的电子书.文档PPT.手册.视频教程等等.而下载的媒介是用微博的微盘,至于为什么选择微盘的,那是因为和微博关联起来,通过微盘上 ...
- ChRoomtst
https://github.com/JawaJedi/ChRoomtst
- TP复习4
<script> window.onload=function(){ if(<{$data.sex}>==0){ document.getElementsByName('sex ...