MySQL學習筆記之二

數據庫的操做總結就是:增刪改查(CURD),今天記錄一下基礎的檢索查詢工做。mysql

檢索MySQLgit

1.查詢表中全部的記錄

mysql> select * from apps;
+----+------------+-----------------------+---------+
| id | app_name   | url                   | country |
+----+------------+-----------------------+---------+
|  1 | QQ APP     | http://im.qq.com      | CN      |
|  2 | 微博 APP   | http://weibo.com      | CN      |
|  3 | 淘寶 APP   | http://www.taobao.com | CN      |
+----+------------+-----------------------+---------+
3 rows in set (0.00 sec)

2. 查詢表中某列(單列)的記錄

mysql> select app_name from apps;
+------------+
| app_name   |
+------------+
| QQ APP     |
| 微博 APP   |
| 淘寶 APP   |
+------------+
3 rows in set (0.00 sec)

3.檢索表中多列的記錄,列名之間用逗號分開

mysql> select id, app_name from apps;
+----+------------+
| id | app_name   |
+----+------------+
|  1 | QQ APP     |
|  2 | 微博 APP   |
|  3 | 淘寶 APP   |
+----+------------+
3 rows in set (0.00 sec)

4.檢索不重複的記錄,distinct關鍵字

mysql> select * from apps;
+----+------------+-----------------------+---------+
| id | app_name   | url                   | country |
+----+------------+-----------------------+---------+
|  1 | QQ APP     | http://im.qq.com      | CN      |
|  2 | 微博 APP   | http://weibo.com      | CN      |
|  3 | 淘寶 APP   | http://www.taobao.com | CN      |
|  4 | QQ APP     | http://im.qq.com      | CN      |
+----+------------+-----------------------+---------+
4 rows in set (0.00 sec)
上面表中是全部的數據,能夠看到第1條和第4條數據是同樣的,若是想要檢索時不想出現重複的數據,能夠使用distinct關鍵字,而且須要放在全部列的前面
mysql> select distinct app_name from apps;
+------------+
| app_name   |
+------------+
| QQ APP     |
| 微博 APP   |
| 淘寶 APP   |
+------------+
3 rows in set (0.00 sec)

5.限制檢索記錄的數量, limit關鍵字

mysql> select * from apps limit 2;
+----+------------+------------------+---------+
| id | app_name   | url              | country |
+----+------------+------------------+---------+
|  1 | QQ APP     | http://im.qq.com | CN      |
|  2 | 微博 APP   | http://weibo.com | CN      |
+----+------------+------------------+---------+
2 rows in set (0.00 sec)

limit後面能夠跟兩個值, 第一個爲起始位置, 第二個是要檢索的行數
mysql> select * from apps limit 1, 2;
+----+------------+-----------------------+---------+
| id | app_name   | url                   | country |
+----+------------+-----------------------+---------+
|  2 | 微博 APP   | http://weibo.com      | CN      |
|  3 | 淘寶 APP   | http://www.taobao.com | CN      |
+----+------------+-----------------------+---------+
2 rows in set (0.00 sec)

5.limit關鍵字和offset配合使用

limit能夠配合offset使用, 如limit 2 offset 1(從行1開始後的2行,默認行數的角標爲0)
mysql> select * from apps limit 2 offset 1;
+----+------------+-----------------------+---------+
| id | app_name   | url                   | country |
+----+------------+-----------------------+---------+
|  2 | 微博 APP   | http://weibo.com      | CN      |
|  3 | 淘寶 APP   | http://www.taobao.com | CN      |
+----+------------+-----------------------+---------+
2 rows in set (0.00 sec)

個人網站:https://wayne214.github.iogithub

相關文章
相關標籤/搜索