2.1 SELECT语句
SELECT语句的用途是从一个或多个表中检索信息。
关键字(keyword)
作为SQL组成部分的保留字。关键字不能作为表或列的名字。2.2 检索单个列
多条SQL语句必须以分号(;)分隔。建议单条的SQL语句后加分号,虽然在某些DBMS不一定需要。
SQL语句不区分大小写。对SQL关键字使用大写,而对表名和列名区分大小写,是为了是语句更易于阅读和调试。 在处理SQL语句时,其中所用空格都被忽略。也就是说,一个SQL语句可以通过空格分开写在多行。MariaDB [sqlbzbh]> SELECT prod_name -> FROM Products;+---------------------+| prod_name |+---------------------+| Fish bean bag toy || Bird bean bag toy || Rabbit bean bag toy || 8 inch teddy bear || 12 inch teddy bear || 18 inch teddy bear || Raggedy Ann || King doll || Queen doll |+---------------------+9 rows in set (0.00 sec)MariaDB [sqlbzbh]>
2.3 检索多个列
在选择多个列时,一定要在列名之间加上逗号,但最后一个列名后不加。
MariaDB [sqlbzbh]> SELECT prod_id, prod_name, prod_price FROM Products;+---------+---------------------+------------+| prod_id | prod_name | prod_price |+---------+---------------------+------------+| BNBG01 | Fish bean bag toy | 3.49 || BNBG02 | Bird bean bag toy | 3.49 || BNBG03 | Rabbit bean bag toy | 3.49 || BR01 | 8 inch teddy bear | 5.99 || BR02 | 12 inch teddy bear | 8.99 || BR03 | 18 inch teddy bear | 11.99 || RGAN01 | Raggedy Ann | 4.99 || RYL01 | King doll | 9.49 || RYL02 | Queen doll | 9.49 |+---------+---------------------+------------+9 rows in set (0.00 sec)MariaDB [sqlbzbh]>
2.4 检索所有列
在实际列名位置使用星号*(通配符)可以检索所有的列。
除非确实需要表中的每一列,否则最好别使用通配符,因为检索不需要的列通常会降低检索和应用程序的性能。MariaDB [sqlbzbh]> SELECT * FROM Products;+---------+---------+---------------------+------------+-----------------------------------------------------------------------+| prod_id | vend_id | prod_name | prod_price | prod_desc |+---------+---------+---------------------+------------+-----------------------------------------------------------------------+| BNBG01 | DLL01 | Fish bean bag toy | 3.49 | Fish bean bag toy, complete with bean bag worms with which to feed it || BNBG02 | DLL01 | Bird bean bag toy | 3.49 | Bird bean bag toy, eggs are not included || BNBG03 | DLL01 | Rabbit bean bag toy | 3.49 | Rabbit bean bag toy, comes with bean bag carrots || BR01 | BRS01 | 8 inch teddy bear | 5.99 | 8 inch teddy bear, comes with cap and jacket || BR02 | BRS01 | 12 inch teddy bear | 8.99 | 12 inch teddy bear, comes with cap and jacket || BR03 | BRS01 | 18 inch teddy bear | 11.99 | 18 inch teddy bear, comes with cap and jacket || RGAN01 | DLL01 | Raggedy Ann | 4.99 | 18 inch Raggedy Ann doll || RYL01 | FNG01 | King doll | 9.49 | 12 inch king doll with royal garments and crown || RYL02 | FNG01 | Queen doll | 9.49 | 12 inch queen doll with royal garments and crown |+---------+---------+---------------------+------------+-----------------------------------------------------------------------+9 rows in set (0.00 sec)MariaDB [sqlbzbh]>
2.5 检索不同的值
使用DISTINCT关键字可以指示数据库只返回不同的值。
如果使用DISTNCT关键字,必须直接放在列名的前面。 DISTINCT关键字作用于所有的列,所有列构成的唯一的行都会被检索出来。MariaDB [sqlbzbh]> SELECT vend_id FROM Products;+---------+| vend_id |+---------+| BRS01 || BRS01 || BRS01 || DLL01 || DLL01 || DLL01 || DLL01 || FNG01 || FNG01 |+---------+9 rows in set (0.00 sec)MariaDB [sqlbzbh]> MariaDB [sqlbzbh]> SELECT DISTINCT vend_id FROM Products;+---------+| vend_id |+---------+| BRS01 || DLL01 || FNG01 |+---------+3 rows in set (0.00 sec)MariaDB [sqlbzbh]> MariaDB [sqlbzbh]> SELECT DISTINCT vend_id, prod_price FROM Products;+---------+------------+| vend_id | prod_price |+---------+------------+| DLL01 | 3.49 || BRS01 | 5.99 || BRS01 | 8.99 || BRS01 | 11.99 || DLL01 | 4.99 || FNG01 | 9.49 |+---------+------------+6 rows in set (0.00 sec)MariaDB [sqlbzbh]>
2.6 限制结果
在MySQL和MariaDB,使用LIMIT子句来限制最多返回多少行。
LIMIT指定返回的行数。LIMIT带的OFFSET指定从哪儿开始。 注意,第一个被检索的行是第0行,而不是第1行。 MySQL支持简化版的LIMIT 4 OFFSET 3语句,即LIMIT 3, 4。MariaDB [sqlbzbh]> SELECT prod_name FROM Products LIMIT 5;+---------------------+| prod_name |+---------------------+| Fish bean bag toy || Bird bean bag toy || Rabbit bean bag toy || 8 inch teddy bear || 12 inch teddy bear |+---------------------+5 rows in set (0.00 sec)MariaDB [sqlbzbh]> MariaDB [sqlbzbh]> SELECT prod_name FROM Products LIMIT 5 OFFSET 5;+--------------------+| prod_name |+--------------------+| 18 inch teddy bear || Raggedy Ann || King doll || Queen doll |+--------------------+4 rows in set (0.00 sec)
2.7 使用注释
注释使用--(两个连字符)嵌在行内。--之后的文本就是注释。
另外形式的注释(很少得到支持)- # 单行注释
- 从/*开始,到*/结束,之间的任何内容都是注释