資料庫查詢語句select用於從表中選取數據,結果被儲存在一個結果表中(稱為結果集);語法格式為「SELECT [列名稱] FROM 表名稱[WHERE子句的等限制條件];」。
(推薦教學:mysql影片教學)
SELECT 語句
SELECT 語句用於從表格中選取資料。
結果被儲存在一個結果表中(稱為結果集)。
SELECT 的語法格式如下:
SELECT {* | <字段列名>} [ FROM <表 1>, <表 2>… [WHERE <表达式> [GROUP BY <group by definition> [HAVING <expression> [{<operator> <expression>}…]] [ORDER BY <order by definition>] [LIMIT[<offset>,] <row count>] ]
其中,各條子句的意義如下:
使用「*」查詢表格的所有欄位
SELECT 可以使用「*」查找表中所有欄位的數據,語法格式如下:SELECT * FROM 表名;
mysql> use test_db; Database changed mysql> SELECT * FROM tb_students_info; +----+--------+---------+------+------+--------+------------+ | id | name | dept_id | age | sex | height | login_date | +----+--------+---------+------+------+--------+------------+ | 1 | Dany | 1 | 25 | F | 160 | 2015-09-10 | | 2 | Green | 3 | 23 | F | 158 | 2016-10-22 | | 3 | Henry | 2 | 23 | M | 185 | 2015-05-31 | | 4 | Jane | 1 | 22 | F | 162 | 2016-12-20 | | 5 | Jim | 1 | 24 | M | 175 | 2016-01-15 | | 6 | John | 2 | 21 | M | 172 | 2015-11-11 | | 7 | Lily | 6 | 22 | F | 165 | 2016-02-26 | | 8 | Susan | 4 | 23 | F | 170 | 2015-10-01 | | 9 | Thomas | 3 | 22 | M | 178 | 2016-06-07 | | 10 | Tom | 4 | 23 | M | 165 | 2016-08-05 | +----+--------+---------+------+------+--------+------------+ 10 rows in set (0.26 sec)
查詢表中指定的欄位
查詢表中的某一個欄位的語法格式為:SELECT < 列名 > FROM < 表名 >;
mysql> SELECT name FROM tb_students_info; +--------+ | name | +--------+ | Dany | | Green | | Henry | | Jane | | Jim | | John | | Lily | | Susan | | Thomas | | Tom | +--------+ 10 rows in set (0.00 sec)
SELECT <字段名1>,<字段名2>,…,<字段名n> FROM <表名>;
mysql> SELECT id,name,height -> FROM tb_students_info; +----+--------+--------+ | id | name | height | +----+--------+--------+ | 1 | Dany | 160 | | 2 | Green | 158 | | 3 | Henry | 185 | | 4 | Jane | 162 | | 5 | Jim | 175 | | 6 | John | 172 | | 7 | Lily | 165 | | 8 | Susan | 170 | | 9 | Thomas | 178 | | 10 | Tom | 165 | +----+--------+--------+ 10 rows in set (0.00 sec)
以上是資料庫查詢語句select有什麼用?的詳細內容。更多資訊請關注PHP中文網其他相關文章!