SQLServer学习笔记(3)基本语法

TOP子句:

SELECT TOP number|percent column_name(s)

FROM table_name

示例:

SELECT TOP 2 * FROM Persons

SELECT TOP 50 PERCENT * FROM Persons

 


LIKE 操作符:

SELECT column_name(s)

FROM table_name

WHERE column_name LIKE pattern

示例:

SELECT * FROM Persons

WHERE City LIKE 'N%'



SELECT * FROM Persons

WHERE City LIKE '%lon%'



SELECT * FROM Persons

WHERE City NOT LIKE '%lon%'

 

SQL通配符:

通配符

描述

%

替代一个或多个字符

_

仅替代一个字符

[charlist]

字符列中的任何单一字符

[^charlist]

或者

[!charlist]

不在字符列中的任何单一字符

示例:

LIKE 'Ne%' 
LIKE '_eorge' 
LIKE 'C_r_er' 
LIKE '[!ALN]%'

 


IN 操作符:

SELECT column_name(s)

FROM table_name

WHERE column_name IN (value1,value2,...)

 

IN操作符允许WHERE子句中规定多个值

 

BETWEEN 操作符:

SELECT column_name(s)

FROM table_name

WHERE column_name

(NOT) BETWEEN value1 AND value2

(不同数据库对于BETWEEN......AND操作是否包括两端值的情况不同,需要验证)

 


Alias语法:

  • 表的别名:

SELECT column_name(s)

FROM table_name

AS alias_name

示例:

SELECT po.OrderID, p.LastName, p.FirstName

FROM Persons AS p, Product_Orders AS po

WHERE p.LastName='Adams' AND p.FirstName='John'

 

  • 列的别名:

SELECT column_name AS alias_name

FROM table_name

示例:

SELECT LastName AS Family, FirstName AS Name

FROM Persons

 


(INENER) JOIN 关键字:

SELECT column_name(s)

FROM table_name1

INNER JOIN table_name2

ON table_name1.column_name=table_name2.column_name

 

用于根据两个或多个表中的列之间的关系,从这些表中查询数据

 

示例:

SELECT Persons.LastName, Persons.FirstName, Orders.OrderNo

FROM Persons

INNER JOIN Orders

ON Persons.Id_P = Orders.Id_P

ORDER BY Persons.LastName

等同于:

SELECT Persons.LastName, Persons.FirstName, Orders.OrderNo

FROM Persons, Orders

WHERE Persons.Id_P = Orders.Id_P

 

  • JOIN: 如果表中有至少一个匹配,则返回行
  • LEFT JOIN: 即使右表中没有匹配,也从左表返回所有的行
  • RIGHT JOIN: 即使左表中没有匹配,也从右表返回所有的行
  • FULL JOIN: 只要其中一个表中存在匹配,就返回行

 

LEFT JOIN 关键字:

SELECT column_name(s)

FROM table_name1

LEFT JOIN table_name2

ON table_name1.column_name=table_name2.column_name

LEFT JOIN 关键字会从左表 (table_name1) 那里返回所有的行,即使在右表 (table_name2) 中没有匹配的行

 

RIGHT JOIN 关键字:

SELECT column_name(s)

FROM table_name1

RIGHT JOIN table_name2

ON table_name1.column_name=table_name2.column_name

RIGHT JOIN 关键字会右表 (table_name2) 那里返回所有的行,即使在左表 (table_name1) 中没有匹配的行

 

FULL JOIN 关键字:

SELECT column_name(s)

FROM table_name1

FULL JOIN table_name2

ON table_name1.column_name=table_name2.column_name

只要其中某个表存在匹配,FULL JOIN 关键字就会返回行


UNION 操作符:

SELECT column_name(s) FROM table_name1

UNION

SELECT column_name(s) FROM table_name2

用于合并两个或多个 SELECT 语句的结果集,UNION 内部的 SELECT 语句必须拥有相同数量的列。列也必须拥有相似的数据类型。同时,每条 SELECT 语句中的列的顺序必须相同

 

UNION ALL 操作符:

SELECT column_name(s) FROM table_name1

UNION ALL

SELECT column_name(s) FROM table_name2

 

默认地,UNION 操作符选取不同的值。如果允许重复的值,请使用 UNION ALL

 


INSERT INTO 语句:

SELECT column_name(s)

INTO new_table_name [IN externaldatabase]

FROM old_tablename

常用于创建表的备份复件或者用于对记录进行存档

示例:

SELECT *

INTO Persons IN 'Backup.mdb'

FROM Persons



SELECT LastName,Firstname

INTO Persons_backup

FROM Persons

WHERE City='Beijing'



SELECT Persons.LastName,Orders.OrderNo

INTO Persons_Order_Backup

FROM Persons

INNER JOIN Orders

ON Persons.Id_P=Orders.Id_P

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值