内容发布更新时间 : 2024/11/2 23:40:46星期一 下面是文章的全部内容请认真阅读。
目 录
1. Select 6. Order by 11. Insert Into 16. Drop Table 21. avg 26. floor 31. tan 36. sign 2. Where 7. Group by 12. Update 17.Create Database 22. max 27. cos 32.tanh 37. sqrt 3. And & Or 8. Having 13. Delete 19. count 23. min 28. cosh 33.exp 4. Between…And 5. Distinct 9. Join 14. Create Table 10. Alias 15. Alter Table 18.Drop Database 20. sum 24. abs 25. ceil 29. sin 34.log 30. sinh 35 power
Select 用途:
从指定表中取出指定的列的数据 语法: 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 ] [ HAVING search_condition ]
[ ORDER BY order_expression [ ASC | DESC ] ]
例:
“Persons” 表中的数据有 LastName Hansen Svendson Pettersen FirstName Ola Tove Kari Address Timoteivn 10 Borgvn 23 Storgt 20 City Sandnes Sandnes Stavanger 选出字段名” LastName”、” FirstName” 的数据 SELECT LastName,FirstName FROM Persons 返回结果: LastName Hansen Svendson Pettersen FirstName Ola Tove Kari 选出所有字段的数据
SELECT * FROM Persons 返回结果:
LastName Hansen Svendson Pettersen FirstName Ola Tove Kari Address Timoteivn 10 Borgvn 23 Storgt 20 City Sandnes Sandnes Stavanger
Where 返回 用途:
被用来规定一种选择查询的标准 语法:
SELECT column FROM table WHERE column condition value 下面的操作符能被使用在WHERE中: =,<>,>,<,>=,<=,BETWEEN,LIKE
注意: 在某些SQL的版本中不等号< >能被写作为!=
解释:
SELECT语句返回WHERE子句中条件为true的数据 例:
从” Persons”表中选出生活在” Sandnes” 的人
SELECT * FROM Persons WHERE City='Sandnes' \表中的数据有:
LastName Hansen Svendson Svendson Pettersen FirstName Ola Tove Stale Kari Address Timoteivn 10 Borgvn 23 Kaivn 18 Storgt 20 City Sandnes Sandnes Sandnes Stavanger Year 1951 1978 1980 1960 返回结果:
LastName Hansen Svendson Svendson FirstName Ola Tove Stale Address Timoteivn 10 Borgvn 23 Kaivn 18 City Sandnes Sandnes Sandnes Year 1951 1978 1980
And & Or 返回 用途:
在WHERE子句中AND和OR被用来连接两个或者更多的条件 解释:
AND在结合两个布尔表达式时,只有在两个表达式都为 TRUE 时才传回 TRUE OR在结合两个布尔表达式时,只要其中一个条件为 TRUE 时,OR便传回 TRUE 例:
\表中的原始数据: LastName Hansen Svendson Svendson FirstName Ola Tove Stephen Address Timoteivn 10 Borgvn 23 Kaivn 18 City Sandnes Sandnes Sandnes 用AND运算子来查找\表中FirstName为”Tove”而且LastName为” Svendson”的数据
SELECT * FROM Persons WHERE FirstName='Tove' AND LastName='Svendson' 返回结果:
LastName Svendson FirstName Tove Address Borgvn 23 City Sandnes 用OR运算子来查找\表中FirstName为”Tove”或者LastName为” Svendson”的数据
SELECT * FROM Persons WHERE firstname='Tove' OR lastname='Svendson' 返回结果:
LastName Svendson Svendson FirstName Tove Stephen Address Borgvn 23 Kaivn 18 City Sandnes Sandnes 你也能结合AND和OR (使用括号形成复杂的表达式),如: SELECT * FROM Persons WHERE (FirstName='Tove' OR FirstName='Stephen') AND LastName='Svendson' 返回结果: