什么是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表达式的灵活使用的更多相关文章

  1. Azure SQL 数据库的灵活缩放预览版简介

    Eron Kelly SQL Server 产品管理部门产品市场营销总经理 几天前,我们宣布了发布 Azure SQL 数据库的灵活缩放公共预览版.新增的灵活缩放功能通过简化开发和管理,简化了扩展和缩 ...

  2. SQLAlchemy 学习笔记(一):Engine 与 SQL 表达式语言

    个人笔记,如有错误烦请指正. SQLAlchemy 是一个用 Python 实现的 ORM (Object Relational Mapping)框架,它由多个组件构成,这些组件可以单独使用,也能独立 ...

  3. mybatis 动态sql表达式相关应用

    一.mybatis 表达式简介 对于mybatis3 ,提供了一种动态sql的方式.通过动态sql我们可以直接在mybatis 的xm映射文件中直接通过条件判断的方式进行查询添加的拼接.mybatis ...

  4. 【JEECG技术文档】数据权限自定义SQL表达式用法说明

    功能介绍   数据规则通过配置自定义sql来实现数据权限的控制,自定义SQL支持表达式取值 其中自定义sql 条件中字段的名称和数据库表的字段名保持一致. 角色授权 用户角色授权,权限测试不要用adm ...

  5. 使用MyBatis的动态SQL表达式时遇到的“坑”(integer)

    现有一项目,ORM框架使用的MyBatis,在进行列表查询时,选择一状态(值为0)通过动态SQL拼接其中条件但无法返回正常的查询结果,随后进行排查. POJO private Integer stat ...

  6. JDK8新特性02 Lambda表达式02_Lambda语法规则

    //函数式接口:只有一个抽象方法的接口称为函数式接口. 可以使用注解 @FunctionalInterface 修饰 @FunctionalInterface public interface MyF ...

  7. 02 SQL 执行

    sql 被保存在 share pool 后, 开始解析, 解析包括语句的语法, 检验及对象, 以确认该用于是否有该对象的权限, 如果这些都通过了, 接下来就要看这个语句之前是否被执行过, 如果是, o ...

  8. C#3.0新增功能09 LINQ 标准查询运算符 02 查询表达式语法

    连载目录    [已更新最新开发文章,点击查看详细] 某些使用更频繁的标准查询运算符具有专用的 C# 语言关键字语法,使用这些语法可以在查询表达式中调用这些运算符. 查询表达式是比基于方法的等效项更具 ...

  9. javaScript基础-02 javascript表达式和运算符

    一.原始表达式 原始表达式是表达式的最小单位,不再包含其他表达式,包含常量,直接量,关键字和变量. 二.对象和数组的初始化表达式 对象和数组初始化表达式实际上是一个新创建的对象和数组. 三.函数表达式 ...

随机推荐

  1. System.Data.SqlTypes.SqlNullValueException: 数据为空。不能对空值调用此方法或

    有可能读出的数据为NULL,可以这样改: 方法一:while (reader.Read()){ for (int i = 0; i < 7; i++) { if (reader.IsDBNull ...

  2. maven3实战之maven使用入门(使用archetype生成项目骨架)

    maven3实战之maven使用入门(使用archetype生成项目骨架) ---------- maven提供了archetype以帮助我们快速勾勒出项目骨架.以Hello World为例,我们使用 ...

  3. Servlet容器的启动过程

    [http://book.51cto.com/art/201408/448854.htm]   Tomcat的启动逻辑是基于观察者模式设计的,所有的容器都会继承Lifecycle接口,它管理着容器的整 ...

  4. [转] [Visual Studio 2012] 找回 建立單元測試 選單

    原文链接:http://www.dotblogs.com.tw/yc421206/archive/2013/03/08/95920.aspx Step1.建立選單 在VS2012選單,Tools→Cu ...

  5. CSS选择器 使用小结

    ==> .x-boundlist-floating[style$="px;"] .x-boundlist-item 元素中含有Style属性,并且属性值以"px;& ...

  6. HDU1028 Ignatius and the Princess III 【母函数模板题】

    Ignatius and the Princess III Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K ...

  7. android学习记录(十三)Task 和 Activity 回退栈操作。

    首先说一下Task是一个什么概念吧:Task是一个包括activity的列表.没 错.简单的说就是依照启动的先后来排队的一个队列.Back Stack.就是回退栈的意思:那么有什么用?Back Sta ...

  8. 模拟log4j获取日志对象调用所在的类名、方法名及行号

    当我们在记录日志时,每个类中会定义一个日志对象,然后利用这个对象去写日志,那么我们在处理日志时,如何能才能记录日志对象所在的类.方法和行号呢?log4j中已经实现了该功能,那么它是怎么实现的呢? 其实 ...

  9. Editor Scripting学习笔记之Menu Item

    主要用到: MenuItem属性 MenuCommand参数 可能用到: EditorApplication类 Selection类 GameObjectUtility类 FileUtil类 Undo ...

  10. Unity3D中使用Leap Motion进行手势控制

    Leap Motion作为一款手势识别设备,相比于Kniect,长处在于准确度. 在我的毕业设计<场景漫游器>的开发中.Leap Motion的手势控制作为重要的一个环节.以此,谈谈开发中 ...