SQL语法手册

发布 2021-05-11 05:10:28 阅读 6309

用途: 从指定表中取出指定的列的数据。

语法:select column_name(s) from table_name

解释:从数据库中选取资料列,并允许从一或多个资料表中,选取一或多个资料列或资料行。select 陈述式的完整语法相当复杂,但主要子句可摘要为:

select select_list

into new_table ]

from table_source

where search_condition ]

group by group_by_expression ]

h**ing search_condition ]

order by order_expression [ asc | desc ]

例: “persons” 表中的数据有。

lastname, firstname, address, city

hansen, ola, timoteivn 10, sandnes

svendson, tove, bor**n 23, sandnes

pettersen, kari, storgt 20, st**anger

选出字段名” lastname”、”firstname” 的数据。

select lastname,firstname from persons

返回结果:lastname, firstname

hansen, ola

svendson, tove

pettersen, kari

选出所有字段的数据。

select * from persons

返回结果:lastname, firstname, address, city

hansen, ola, timoteivn 10, sandnes

svendson, tove, bor**n 23, sandnes

pettersen, kari, storgt 20, st**anger

where用途:被用来规定一种选择查询的标准。

语法:select column from table where column condition value

下面的操作符能被使用在where中:,

注意: 在某些sql的版本中不等号< >能被写作为!=

解释: select语句返回where子句中条件为true的数据。

例:从” persons”表中选出生活在” sandnes” 的人。

select * from persons where city='sandnes'

"persons" 表中的数据有:

lastname, firstname, address, city, year

hansen, ola, timoteivn 10, sandnes, 1951

svendson, tove, bor**n 23, sandnes, 1978

svendson, stale, kaivn 18, sandnes, 1980

pettersen, kari, storgt 20, st**anger, 1960

返回结果:lastname, firstname, address, city, year

hansen, ola, timoteivn 10, sandnes, 1951

svendson, tove, bor**n 23, sandnes, 1978

svendson, stale, kaivn 18, sandnes, 1980

and & or

用途:在where子句中and和or被用来连接两个或者更多的条件。

解释:and在结合两个布尔表达式时,只有在两个表达式都为 true 时才传回 true

or在结合两个布尔表达式时,只要其中一个条件为 true 时,or便传回 true

例:lastname, firstname, address, city

hansen, ola, timoteivn 10, sandnes

svendson, tove, bor**n 23, sandnes

svendson, stephen, kaivn 18, sandnes

用and运算子来查找"persons" 表中firstname为”tove”而且lastname为” svendson”的数据。

select * from persons

where firstname='tove'

and lastname='svendson'

返回结果:

lastname, firstname, address, city

svendson, tove, bor**n 23, sandnes

用or运算子来查找"persons" 表中firstname为”tove”或者lastname为” svendson”的数据。

select * from persons

where firstname='tove'

or lastname='svendson'

返回结果:

lastname, firstname, address, city

svendson, tove, bor**n 23, sandnes

svendson, stephen, kaivn 18, sandnes

你也能结合and和or (使用括号形成复杂的表达式),如:

select * from persons where

firstname='tove' or firstname='stephen')

and lastname='svendson'

返回结果:lastname, firstname, address, city

svendson, tove, bor**n 23, sandnes

svendson, stephen, kaivn 18, sandnes

between…and

用途: 指定需返回数据的范围。

语法:select column_name from table_name

where column_name

between value1 and value2

例:“persons”表中的原始数据。

lastname, firstname, address, city

hansen, ola, timoteivn 10, sandnes

nordmann, anna, neset 18, sandnes

pettersen, kari, storgt 20, st**anger

svendson, tove, bor**n 23, sandnes

用between…and返回lastname为从”hansen”到”pettersen”的数据:

select * from persons where lastname

between 'hansen' and 'pettersen'

返回结果:lastname, firstname, address, city

hansen, ola, timoteivn 10, sandnes

nordmann, anna, neset 18, sandnes

pettersen, kari, storgt 20, st**anger

为了显示指定范围之外的数据,也可以用not操作符:

select * from persons where lastname

not between 'hansen' and 'pettersen'

返回结果:lastname, firstname, address, city

svendson, tove, bor**n 23, sandnes

用途: distinct关键字被用作返回唯一的值。

语法:select distinct column-name(s) from table-name

解释: 当column-name(s)中存在重复的值时,返回结果仅留下一个。

例:“orders”表中的原始数据。

company, ordernumber

sega, 3412

w3schools, 2312

trio, 4678

w3schools, 6798

用distinct关键字返回company字段中唯一的值:

select distinct company from orders

返回结果:company

segaw3schools

trioorder by

用途:指定结果集的排序。

语法:select column-name(s) from table-name order by

解释: 指定结果集的排序,可以按照asc(递增方式排序,从最低值到最高值)或者desc(递减方式排序,从最高值到最低值)的方式进行排序,默认的方式是asc

例:“orders”表中的原始数据:

company, ordernumber

sega, 3412

abc shop, 5678

w3schools, 2312

w3schools, 6798

按照company字段的升序方式返回结果集:

select company, ordernumber from orders

order by company

返回结果:

company, ordernumber

abc shop, 5678

sega, 3412

w3schools, 6798

w3schools, 2312

按照company字段的降序方式返回结果集:

select company, ordernumber from orders

order by company desc

返回结果:company, ordernumber

w3schools, 6798

w3schools, 2312

sega, 3412

abc shop, 5678

group by

用途:对结果集进行分组,常与汇总函数一起使用。

SQL语法手册

用途 从指定表中取出指定的列的数据。语法 解释 从数据库中选取资料列,并允许从一或多个资料表中,选取一或多个资料列或资料行。select 陈述式的完整语法相当复杂,但主要子句可摘要为 select select list into new table from table source where ...

SQL语法手册

用途 从指定表中取出指定的列的数据。语法 解释 从数据库中选取资料列,并允许从一或多个资料表中,选取一或多个资料列或资料行。select陈述式的完整语法相当复杂,但主要子句可摘要为 select select list into new table from table source where s...

sql语法

知识要点 1 transact sql运算符 1 算术运算符。注 null与任何值运算结果为null。运算可用于datetime型数据。2 字符串运算符。3 比较运算符。逻辑常量 ture false。4 逻辑运算符。not 非 and 与 or 或 between 指定范围 like 模糊匹配 5...