鍍金池/ 教程/ 數(shù)據(jù)庫(kù)/ MariaDB查詢數(shù)據(jù)
MariaDB比較運(yùn)算符
MariaDB查詢數(shù)據(jù)
MariaDB Like子句
MariaDB Sum()函數(shù)
MariaDB過程
MariaDB限制返回記錄
MariaDB Count()函數(shù)
MariaDB更新數(shù)據(jù)
MariaDB導(dǎo)出數(shù)據(jù)
MariaDB Intersect運(yùn)算符
MariaDB函數(shù)
MariaDB Min()函數(shù)
MariaDB創(chuàng)建數(shù)據(jù)庫(kù)
MariaDB Avg()函數(shù)
MariaDB刪除數(shù)據(jù)
MariaDB條件
MariaDB功能特點(diǎn)
MariaDB創(chuàng)建表
MariaDB左外連接
MariaDB Union運(yùn)算符
MariaDB安裝
MariaDB選擇數(shù)據(jù)庫(kù)
MariaDB Max()函數(shù)
MariaDB Where子句
MariaDB右外連接
MariaDB內(nèi)連接
MariaDB截?cái)啾?/span>
MariaDB Order By子句
MariaDB教程
MariaDB正則表達(dá)式
MariaDB Union All運(yùn)算符
MariaDB Distinct子句
MariaDB修改表
MariaDB刪除表
MariaDB插入數(shù)據(jù)
MariaDB From子句
MariaDB刪除數(shù)據(jù)庫(kù)
MariaDB簡(jiǎn)介
MariaDB數(shù)據(jù)類型

MariaDB查詢數(shù)據(jù)

SELECT語(yǔ)句用于從單個(gè)或多個(gè)表中檢索記錄。

語(yǔ)法

SELECT expressions  
FROM tables  
[WHERE conditions];

SELECT語(yǔ)句可以與UNION語(yǔ)句,ORDER BY子句,LIMIT子句,WHERE子句,GROUP BY子句,HAVING子句等一起使用。如下語(yǔ)法 -

SELECT [ ALL | DISTINCT ]  
expressions  
FROM tables  
[WHERE conditions]  
[GROUP BY expressions]  
[HAVING condition]  
[ORDER BY expression [ ASC | DESC ]];

1. 從表中選擇所有列

示例:

我們有一個(gè)表students,有一些數(shù)據(jù)。 因此,從students中檢索所有記錄。參考以下查詢語(yǔ)句 -

SELECT * FROM students;

執(zhí)行上面查詢語(yǔ)句,得到以下結(jié)果 -

MariaDB [testdb]> SELECT * FROM Students;
+------------+--------------+-----------------+----------------+
| student_id | student_name | student_address | admission_date |
+------------+--------------+-----------------+----------------+
|          1 | Maxsu        | Haikou          | 2017-01-07     |
|          3 | JMaster      | Beijing         | 2016-05-07     |
|          4 | Mahesh       | Guangzhou       | 2016-06-07     |
|          5 | Kobe         | Shanghai        | 2016-02-07     |
|          6 | Blaba        | Shengzheng      | 2016-08-07     |
+------------+--------------+-----------------+----------------+
5 rows in set (0.00 sec)

2. 從表中選擇指定列

可以使用SELECT語(yǔ)句從表中檢索單個(gè)列(指定列)。它有助于您只檢索那些需要的列。

示例:

SELECT student_id, student_name, student_address  
FROM Students  
WHERE student_id < 4  
ORDER BY student_id ASC;

執(zhí)行上面查詢語(yǔ)句,得到以下結(jié)果 -

MariaDB [testdb]> SELECT student_id, student_name, student_address
    -> FROM Students
    -> WHERE student_id < 4
    -> ORDER BY student_id ASC;
+------------+--------------+-----------------+
| student_id | student_name | student_address |
+------------+--------------+-----------------+
|          1 | Maxsu        | Haikou          |
|          3 | JMaster      | Beijing         |
+------------+--------------+-----------------+
2 rows in set (0.18 sec)

在上面查詢語(yǔ)句中,它查詢表student中那些student_id小于4,并選擇student_id,student_namestudent_address列,然后根據(jù)student_id以升序排列行記錄。