当前位置: 首页 > 网络学院 > 服务端脚本教程 > SQL > SQL Where 子句

SQL
SQL 介绍
SQL Select
SQL Where 子句
SQL Insert
SQL Update
SQL Delete
SQL 技能测试
SQL Order By
SQL 与 & OR
SQL In
SQL Between
SQL Aliases
SQL Join
SQL Union
SQL Create
SQL Drop
SQL Alter
SQL 函数
SQL Group By
SQL Select Into

SQL Where 子句


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2009-03-01   浏览: 1017 ::
收藏到网摘: n/a

The WHERE clause is used to specify a selection criterion.
WHERE子句可以规定选择的依据(符合要求才会选中)


WHERE 子句

To conditionally select data from a table, a WHERE clause can be added to the SELECT statement.
为了可以有条件地选择数据,给SELECT声明加上WHERE子句就可以办到

语法

SELECT column FROM table
WHERE column operator value

With the WHERE clause, the following operators can be used:
使用WHERE子句,可以进行下面这些操作:

运算符 描述
= Equal 等于
<> Not equal 不等于
> Greater than 大于
< Less than 小于
>= Greater than or equal 大于等于
<= Less than or equal 小于等于
BETWEEN Between an inclusive range 在多少范围内
LIKE

Search for a pattern 寻找匹配

Note: In some versions of SQL the <> operator may be written as !=
注意:在一些版本的SQL里<>操作符要变成这样的写法 !=


Using the WHERE Clause
使用 WHERE 子句

To select only the persons living in the city "Sandnes", we add a WHERE clause to the SELECT statement:
如果要只选出住在"Sandens"城市里的个人信息,这句SELECT与WHERE结合的语句就应该这么写:

SELECT * FROM Persons
WHERE City='Sandnes'

"Persons" 表

LastName FirstName Address City Year
Hansen Ola Timoteivn 10 Sandnes 1951
Svendson Tove Borgvn 23 Sandnes 1978
Svendson Stale Kaivn 18 Sandnes 1980
Pettersen Kari Storgt 20 Stavanger 1960

结果

LastName FirstName Address City Year
Hansen Ola Timoteivn 10 Sandnes 1951
Svendson Tove Borgvn 23 Sandnes 1978
Svendson Stale Kaivn 18 Sandnes 1980


Using Quotes
使用单引号

Note that we have used single quotes around the conditional values in the examples.
我们在上面那个举例中对条件值使用了单引号。

SQL uses single quotes around text values (most database systems will also accept double quotes). Numeric values should not be enclosed in quotes.
SQL语言中单引号用在文字值上(也有很多数据库系统支持双引号).而数字值就不能使用单引号了

For text values:
先看下文字值的例子:

这句对的:
SELECT * FROM Persons WHERE FirstName='Tove'
这句错了:
SELECT * FROM Persons WHERE FirstName=Tove

再看下数字值的例子:

这句对的:
SELECT * FROM Persons WHERE Year>1965
这句错了:
SELECT * FROM Persons WHERE Year>'1965'


The LIKE Condition
LIKE 条件

The LIKE condition is used to specify a search for a pattern in a column.
LIKE条件一般用在在指定搜索某字段的时候

语法

SELECT column FROM table
WHERE column LIKE pattern

A "%" sign can be used to define wildcards (missing letters in the pattern) both before and after the pattern.
"%"这个符号可以代表通配字符(就是可以代表任何字符),可以放在搜索内容的前面和后面


Using LIKE
使用LIKE

The following SQL statement will return persons with first names that start with an 'O':
下面的这条SQL语句会将所有名内有开头为'O'的个人信息返回出来:

SELECT * FROM Persons
WHERE FirstName LIKE 'O%'

The following SQL statement will return persons with first names that end with an 'a':
下面这句SQL会将名内有结尾是'a'的个人信息返回出来:

SELECT * FROM Persons
WHERE FirstName LIKE '%a'

The following SQL statement will return persons with first names that contain the pattern 'la':
接下来的SQL语句会将名内凡是含有'la'的个人信息返回出来:

SELECT * FROM Persons
WHERE FirstName LIKE '%la%'

评论 (0) All

登陆 | 还没注册?