數據庫的操做總結就是:增刪改查(CURD),今天記錄一下基礎的檢索查詢工做。mysql
檢索MySQLgit
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)
mysql> select app_name from apps; +------------+ | app_name | +------------+ | QQ APP | | 微博 APP | | 淘寶 APP | +------------+ 3 rows in set (0.00 sec)
mysql> select id, app_name from apps; +----+------------+ | id | app_name | +----+------------+ | 1 | QQ APP | | 2 | 微博 APP | | 3 | 淘寶 APP | +----+------------+ 3 rows in set (0.00 sec)
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)
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)
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