资讯专栏INFORMATION COLUMN

MySQL学习笔记之二

Julylovin / 2198人阅读

数据库的操作总结就是:增删改查(CURD),今天记录一下基础的检索查询工作。

检索MySQL
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.io

文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/yun/17882.html

相关文章

  • Codeigniter 4.0-dev 版源码学习笔记之二——入口以及初始化操作

    摘要:通过这个函数可以很方便的在程序运行期间执行很多常见操作。此文可以转载,但转载前需要发邮件到进行沟通,未沟通的均视作侵权。 index.php index.php 是整个框架的入口文件,也就是说所有的请求都要从它这里开始。因为 index.php 源码非常简洁,那么我们直接放一张源码截图,按着截图说一下源码。 showImg(https://segmentfault.com/img/re...

    _ivan 评论0 收藏0

发表评论

0条评论

最新活动
阅读需要支付1元查看
<