SQL Where 子句

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 小于等于
BETWEENBetween 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" 表

LastNameFirstNameAddressCityYear
HansenOlaTimoteivn 10Sandnes1951
SvendsonToveBorgvn 23Sandnes1978
SvendsonStaleKaivn 18Sandnes1980
PettersenKariStorgt 20Stavanger1960

结果

LastNameFirstNameAddressCityYear
HansenOlaTimoteivn 10Sandnes1951
SvendsonToveBorgvn 23Sandnes1978
SvendsonStaleKaivn 18Sandnes1980


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
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值