查询数据(SELECT)

# 查询所有数据 — 很危险,数据量过大,容易导致内存溢出而宕机
mysql> select * from student; # 先查询数据总量,然后决定是否可以查询所有数据
mysql> select count(distinct countrycode) from city;
+-----------------------------+
| count(distinct countrycode) |
+-----------------------------+
| 232 |
+-----------------------------+
1 row in set (0.00 sec) mysql> select count(countrycode) from city;
+--------------------+
| count(countrycode) |
+--------------------+
| 4079 |
+--------------------+
1 row in set (0.00 sec) mysql> select count(*) from city;
+----------+
| count(*) |
+----------+
| 4079 |
+----------+
1 row in set (0.00 sec) # 查询指定列数据
mysql> select user,host from mysql.user;
+--------+------------+
| user | host |
+--------+------------+
| root | % |
| root | 127.0.0.1 |
| lhd | 172.16.1.% |
| zzzwqh | 172.16.1.% |
| root | 172.16.1.% |
| root | ::1 |
| | db03 |
| root | db03 |
| | localhost |
| root | localhost |
+--------+------------+
10 rows in set (0.01 sec)

条件查询(SELECT,WHERE)

mysql> select name,gender from student where name='小王';
+--------+--------+
| name | gender |
+--------+--------+
| 小王 | f |
+--------+--------+
1 row in set (0.00 sec)

查询示例

导入一个 world 数据库,点击下载,解压即可

导入数据(命令行,SOURCE)

# 方式一:
[root@db03 ~]# mysql -uroot -p123 < world.sql # 方式二:
mysql> source /root/world.sql; # 方式三:
mysql> \. /root/world.sql;

查询数据(SELECT,WHERE,COUNT,LIMIT,ORDER BY,DESC)

mysql> use world;
Database changed
mysql> show tables;
+-----------------+
| Tables_in_world |
+-----------------+
| city |
| country |
| countrylanguage |
+-----------------+
3 rows in set (0.00 sec) mysql> select count(*) from city;
+----------+
| count(*) |
+----------+
| 4079 |
+----------+
1 row in set (0.00 sec) mysql> select * from city; # 1.查看表结构
mysql> desc city;
+-------------+----------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------------+----------+------+-----+---------+----------------+
| ID | int(11) | NO | PRI | NULL | auto_increment |
| Name | char(35) | NO | | | |
| CountryCode | char(3) | NO | MUL | | |
| District | char(20) | NO | | | |
| Population | int(11) | NO | | 0 | |
+-------------+----------+------+-----+---------+----------------+
5 rows in set (0.00 sec) # 2.查看所有数据
mysql> select * from city; # 3.查看指定列的数据
mysql> select Name,Population from city; # 4.查看数据时排序(按照人口数量)
# 升序
mysql> select Name,Population from city order by Population;
# 降序
mysql> select Name,Population from city order by Population desc; # 5.查询部分数据
# 查看前十条数据
mysql> select Name,Population from city order by Population desc limit 10; # 6.按照步长查询数据,第一个 50 表示起始位置,第二个 50 表示步长
mysql> select id,Name,Population from city limit 50,50;
# 第一个 50 表示起始位置,第二个 50 表示步长

条件查询(or,in,union all,and,like,=,<,>,<=,>=,!=,<>)

# 1.条件查询就是使用where语句,where语句可以使用的符号
条件符号:= < > <= >= != <> or and like
精确匹配:=
范围匹配:< > <= >= != <>
模糊匹配:like
连接语句:or and # 2.查询中国的城市人口
mysql> select name,population from city where CountryCode='CHN'; # 3.查询黑龙江人口数量
mysql> select name,population from city where countrycode='CHN' and District='heilongjiang'; # 4.查询中国人口数量小于 100000 的城市
mysql> select name,population from city where countrycode='CHN' and population < 100000; # 5.模糊匹配
# 匹配以 N 结尾的数据
mysql> select name,countrycode from city where countrycode like '%N';
# 匹配以 N 开头的数据
mysql> select name,countrycode from city where countrycode like 'N%';
# 匹配包含 N 的数据
mysql> select name,countrycode from city where countrycode like '%N%'; # 6.查询中国或美国的人口数量
# 使用 or
mysql> select name,population from city where countrycode = 'CHN' or countrycode = 'USA';
# 使用 in
mysql> select name,population from city where countrycode in ('CHN','USA');
# 使用 union all,效率最高
mysql> select name,population from city where countrycode = 'CHN' union all select name,population from city where countrycode = 'USA';

最新文章

  1. ubuntu10.04配置XMAPP中的环境变量
  2. Linux中获取本机网络信息的几个函数及应用
  3. Sql 随机更新一条数据返回更新数据的ID编号
  4. c++11:iota
  5. 如何修改UITableView每个cell的分隔线和左边的距离?
  6. [个人原创]关于java中对象排序的一些探讨(二)
  7. 利用raspberry pi搭建typecho笔记(二) sqlite和typecho部署
  8. java major version(转)
  9. 如何把我的Java程序变成exe文件?
  10. [物理学与PDEs]第1章第1节 引言
  11. springboot2.0以后WebMvcConfigurationSupport代替WebMvcConfigurationAdapter
  12. 关于笔记本安装parrot和kali的一些问题(花屏,息屏,屏幕不能休眠)
  13. bugfree3.0.1-修改“优先级”为中文引起的PHP Error
  14. 从函数式编程到Promise
  15. qa_model
  16. MongoDB导入导出以及数据库备份以及.dat数据
  17. 跟据经纬度实现附近搜索Java实现
  18. OC中NSClassFromString()与NSStringFromClass()的用法及应用场景
  19. 【CodeForces】679 A. Bear and Prime 100
  20. C语言指针【转】

热门文章

  1. BAPI_MATERIAL_BOM_GROUP_CREATE创建BOM
  2. JavaScript小记
  3. apscheduler(定时任务) 基于redis持久化配置操作
  4. Android事件分发机制四:学了事件分发有什么用?
  5. 前端面试准备笔记之JavaScript(01)
  6. oblet
  7. pthon之变量
  8. CentOS 7 使用pyenv安装python3.6
  9. springsecurity教程一
  10. Elastic Stack简介和Elasticsearch--先搞清楚概念第二篇