数据库的操做总结就是:增删改查(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