继续前面SQL的基本语法,今天记录了SQL Like, Wildcards, In, Between, Aliases这五个基本语法。
11、Like
The LIKE operator is used in a WHERE clause to search for a specified pattern in a column.
Like操作符一般用在Where从句中,用来寻找一列中的特定模式。
SQL LIKE 语法
SELECT column_name(s) FROM table_name WHERE column_name LIKE pattern;
Example
SELECT * FROM Customers WHERE Country NOT LIKE '%land%';
这段代码是从Customers表中,取出Country名中不包含“land”字符的记录。不是用关键字“NOT”则表示,取出Country名中包含“land”的记录。“%”是一个用来表示其他任意符号的通配符。
12、Wildcards
A wildcard character can be used to substitute for any other character(s) in a string.
In SQL, wildcard characters are used with the SQL LIKE operator.
SQL wildcards are used to search for data within a table.
Wildcard(通配符)可以用来替代字符串中其他的任意符号。在SQL语句中经常和Like操作符一起使用,用来在一张表中寻找满足特定要求的数据。
SQL中用到的通配符有:
Wildcard | Description |
---|---|
% | A substitute for zero or more characters (可以代替0个或多个字符) |
_ | A substitute for a single character (可以替代单个字符) |
[charlist ] | Sets and ranges of characters to match (匹配的字符集或者范围) |
[^charlist ] or [!charlist ] | Matches only a character NOT specified within the brackets (仅匹配单个字符,而且不是括号中所指定的) |
Example
SELECT * FROM Customers WHERE City LIKE '_erlin';
上面这段代码是从Customers的所有记录中,筛选出City名以’erlin’结尾,以任意一个字母开头的记录。
SELECT * FROM Customers WHERE City LIKE '[abc]%';
上面这段代码是从Customers的所有记录中,筛选出City名以’a’, ‘b’或者’c’开头的记录,这里的[abc]还可以写成[a-c]。
SELECT * FROM Customers WHERE City LIKE '[!bsp]%';
or
SELECT * FROM Customers WHERE City NOT LIKE '[bsp]%';
上面这两段代码都是从Customers的所有记录中,筛选出City名不是以’b’, ‘s’或者’p’开头的记录。
13、In
The IN operator allows you to specify multiple values in a WHERE clause.
IN 操作符用在Where从句中,用于指定多个可选的值。
SQL IN 语法
SELECT column_name(s)
FROM table_name
WHERE column_name IN (value1,value2,...);
Example
SELECT * FROM Customers WHERE City IN ('Paris','London');
上述例子是从Customers的所有记录中找出城市名为’Paris’或者’London’的记录。
14、Between
The BETWEEN operator is used to selects values within a range. The values can be numbers, texts, or dates.
Between 操作符用于在一个范围内筛选值,这些值可以是数字,文本或者日期。
SQL BETWEEN 语法
SELECT column_name(s) FROM table_name WHERE column_name BETWEEN value1 AND value2;
Example
SELECT * FROM Products WHERE Price BETWEEN 10 AND 20
上述代码是从 Product s的所有记录中选出 Price 在 10 到 20 之间的记录。下面一个例子是结合 IN 操作符的用法,分别用到了数字、文本和日期值。
SELECT * FROM Products WHERE (Price BETWEEN 10 AND 20) AND NOT CategoryID IN (1,2,3);
SELECT * FROM Products WHERE ProductName BETWEEN 'C' AND 'M';
SELECT * FROM Oders
WHERE OrderDate BETWEEN #07/04/1996# AND #07/09/1996#;
注意:
在不同的数据库中使用 BETWEEN 操作符可能会得到不同的结果,有些筛选只包括Between后面给出的条件范围内的值而不包括两端的值,有些包括条件范围内的值以及两端的值,还有一些包括条件范围内的值和第一个条件值,而不包括最后一个条件值。所以在使用 BETWEEN 的时候需要检查一下是哪种情况。
15、Aliases
SQL Aliases are used to temporarily rename a table or a column heading.
Basically aliases are created to make column names more readable.
Aliases 被用来给表或者表中的某一列指定别名的。
SQL Alias Syntax for Columns
SELECT column_name AS alias_name FROM table_name;
SQL Alias Syntax for Tables
SELECT column_name(s) FROM table_name AS alias_name;
Example
SELECT CustomerName AS Customer, ContactName AS [Contact Person] FROM Customers;
上述代码分别给 CustomerName 和 ContactName 这两列设置了别名。注意,当设置的别名包含空格时,需要用双引号或者方括号括起来。
SELECT CustomerName, Address+', '+City+', '+PostalCode+', '+Country AS Address FROM Customers;
上述代码把(Address, City, PostalCode, Country)四列合并起来命名为”Address”。
SELECT o.OrderID, o.OrderDate, c.CustomerName FROM Customers AS c, Orders AS o WHERE c.CustomerName="Around the Horn" AND c.CustomerID=o.CustomerID;
上述代码对”Customers”和”Orders”这两张表分别设置了别名,并从所有的订单中选出顾客姓名为”Around the Horn”的订单。
下面是同样的代码但是没有设置别名:
SELECT Orders.OrderID, Orders.OrderDate, Customers.CustomerName
FROM Customers, Orders
WHERE Customers.CustomerName="Around the Horn" AND Customers.CustomerID=Orders.CustomerID;
可以看出设置了别名的代码更加简洁。
设置别名(Aliases)主要用于以下场景:
- 在一个查询语句中涉及到不止一张表
- 在查询语句中用到了函数(后面会讲到函数)
- 列名太长了,可读性比较差
- 需要将两个或者多个列合并在一起