【一篇文章就够了】SQL server 简单语法

如果你和楼主一样,有MySql的基础,那么想要学会如何使用SqlServer,这篇文章就够了

子查询

语法:WITH table_name AS alias (select statement)
例子:

        WITH T AS
                 (
                     SELECT 
                            a.*
                     FROM student a 
                 )
        SELECT *
        FROM T

WITH (NOLOCK)

语法:FROM table_name WITH (NOLOCK)
例子:

SELECT a.*
FROM student a WITH (NOLOCK)
WHERE (${sqlWhere})

所有Select加 With (NoLock)解决阻塞死锁,在sqlserver 中with(nolock)详解

stuff 与 for xml path 连用

多条数据合并,https://blog.csdn.net/u011229848/article/details/49930229

CASE

语法:
SELECT CASE input_expression
WHEN when_expression THEN result_expression
WHEN when_expression THEN result_expression
......
ELSE else_result_expression END AS 'alias'
FROM databasename.dbo.tablename
例子:

SELECT CASE sex
           WHEN '女' THEN 1
           WHEN '男' THEN 2
           ELSE 0
           END AS '性别'
FROM sutdent

语法:
SELECT CASE
WHEN boolean_expression THEN result_expression
WHEN boolean_expression THEN result_expression
......
ELSE else_result_expression END AS 'alias'
FROM databasename.dbo.tablename
例子:

SELECT CASE
           WHEN sex = '女' THEN 1
           WHEN sex = '男' THEN 2
           ELSE 0
           END AS '性别'
FROM sutdent

语法:
SELECT column_name = ** CASE**
WHEN boolean_expression/ when_expression THEN result_expression
WHEN boolean_expression/ when_expression THEN result_expression
......
ELSE else_result_expression END AS 'alias'
FROM databasename.dbo.tablename
例子:

SELECT sex = CASE
           WHEN '女' THEN 1
           WHEN '男' THEN 2
           ELSE 0
           END 
FROM sutdent

简单来说就向Java里面的Switch, SQLServer之Case用法

TOP

语法:TOP 1 column_name
例子:

SELECT TOP 13 id FROM VIPPlan;

取前n数字,默认按照column正序排列。如果后跟的不是列名,就赋值。

SELECT TOP 13 'id' FROM VIPPlan;

CONVERT()

CONVERT(VARCHAR(19),GETDATE(),120)
CONVERT(VARCHAR(10),GETDATE(),110) 
CONVERT(VARCHAR(11),GETDATE(),106)
CONVERT(VARCHAR(24),GETDATE(),113)

CONVERT() 函数是把日期转换为新数据类型的通用函数,https://www.w3school.com.cn/sql/func_convert.asp。

GETDATE()

返回的是SQL Server所在服务器的时间

OVER PARTITION BY

PARTITION BY 字段 根据该字段汇总,sql over(partition by) 开窗函数的使用

ROW_NUMBER() OVER()

ROW_NUMBER()就是生成行数的方法,会根据OVER()里面的内容为表生成一列序号。括号里经常使用partition by、order by

https://blog.csdn.net/qq_25221835/article/details/82762416

经常用(ROW_NUMBER() OVER (ORDER BY createTime DESC )) AS rownumber来为表添加序列号。

ISNULL

ISNULL(参数1,参数2),判断参数1是否为NULL,如果是,返回参数2,否则返回参数1。

DATEDIFF

用法:DATEDIFF() 函数返回两个日期之间的时间,例如:

SELECT DATEDIFF(day,'2008-12-29','2008-12-30') AS DiffDate

SELECT DATEDIFF(day,'2008-12-30','2008-12-29') AS DiffDate

BEGIN END

Begin...End通常用来表示一个语句块,其内部的代码可以包含一组T-SQL语句,凡是在这个语句块里的所有代码,都属于同一流程控制。Begin...End语句通常与If、While语句搭配使用。

OUTER APPLY

简单的理解,OUTER APPLY相当于LEFT JOIN,CROSS APPLY相当于INNER JOIN,详细请看https://blog.csdn.net/wikey_zhang/article/details/77480118。

EXISTS

带有Exists的子查询不返回任何记录的数据,只返回逻辑值“True”或“False”,如果为True查询成功,如果为False查询失败,返回空。详细请看https://blog.csdn.net/xiaouncle/article/details/88084889。

计算过程中的保留两位小数的百分比

ltrim(cast(a * 1.0 / b * 100 AS DEC(10, 2))) + '%'

分页

offset ${startrow} rows fetch next ${pagesize} rows only
posted @ 2021-05-11 14:47  朱李洛克  阅读(230)  评论(0编辑  收藏  举报
// 侧边栏目录 // https://blog-static.cnblogs.com/files/douzujun/marvin.nav.my1502.css