什么是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. socket 连接,使得地址马上可以重用

    /* 使地址马上可以重用 */                                                                                     ...

  2. WinForm控件使用文章收藏整理完成

    对C# WinForm开发系列收集的控件使用方面进行整理, 加入了一些文章, 不断补充充实, 完善这方面. 基础 - 常用控件 C# WinForm开发系列 - CheckBox/Button/Lab ...

  3. sql server对并发的处理-乐观锁和悲观锁【粘】

    假如两个线程同时修改数据库同一条记录,就会导致后一条记录覆盖前一条,从而引发一些问题. 例如: 一个售票系统有一个余票数,客户端每调用一次出票方法,余票数就减一. 情景: 总共300张票,假设两个售票 ...

  4. [XAF]如何在非按钮事件中打开视图

    private static void OpenDetailView(XafApplication app) { IObjectSpace os = app.CreateObjectSpace(); ...

  5. k-means聚类JAVA实例

    <mahout in action>第六章. datafile/cluster/simple_k-means.txt数据集例如以下: 1 1 2 1 1 2 2 2 3 3 8 8 8 9 ...

  6. 【JavsScript】webapp的优化整理

    单页or多页 webapp 现状 优劣之分 网络传输优化 综述 fake页-首屏加速 降低请求数 降低请求量 缓存Ajax/localstorage DOM操作优化 综述 关于页面渲染 减少使用定位属 ...

  7. [Angular2 Router] CanActivate Route Guard - An Example of An Asynchronous Route Guard

    In this tutorial we are going to learn how we can to configure an can activate route guard in the An ...

  8. 【React Native 实战】商品分类

    1.前言 商品分类是各种app常见的一种操作,一般都是左右两栏构成,左边栏是商品的分类,右边栏是商品的展示,同时左右两栏都可以滑动.今天我们就用React Native来实现这种效果. 实现内容:1) ...

  9. How to Copy and Paste in the Ubuntu Gnome Terminal

    How to Copy: Select the content in terminal use your mouse , and then use Ctrl + Shift + C to copy t ...

  10. RMI、RPC、SOAP通讯技术介绍及比对 - XML/SOAP

    RMI.RPC.SOAP通信技术介绍及比对 1.RMI 使用java的程序员,对于RMI(RemoteMethod Invoke,远程方法调用)一定不陌生,在java中,为了在分布式应用开发时,能够方 ...