查询数据(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';

DQL 数据查询语言的更多相关文章

  1. 八:SQL之DQL数据查询语言单表操作

    前言: DQL数据库查询语言是我们在开发中最常使用的SQL,这一章总结了单表操作部分的常用查询方式 主要操作有:查询所有字段.查询指定字段.查询指定记录.带IN的关键字查询,范围查询,陪查询.查询空值 ...

  2. 九:SQL之DQL数据查询语言多表操作

    前言: 一:数据准备 员工表emp 和部门表 dept 注意:我在录入员工表的时候,特意添加了两条没有部门的员工,他们的部门id对应为null; --分别创建部门和员工表,并实现一对多关系 DROP ...

  3. DQL 数据查询语言 IS (information_schema)

    3.information_schema 统计信息库 1.介绍: 视图 1.安全: 只允许查询,不知道操作的对象是谁. 2.方便: 只需要简单的select语句即可使用. 2.作用: 1.方便我们做数 ...

  4. DQL 数据查询语言 select

    1.select 1.select 单独使用 (1) 查询数据库的参数 查看端口: select @@port; 查看数据路径 select @@datadir; (2)调用内置函数 查看当前库 se ...

  5. MySQL数据库之DQL(数据查询语言)

    1.MySQL之DQL查询AS CONCAT LIKE的使用 (1)select 列名1,列名2,...... from 表名 [where 条件] 查询所有字段用*,不带where条件的话,就会把表 ...

  6. 第六章 DQL 数据查询语言

    一.select 简单查询命令 #1.查询表中所有的数据 mysql> select * from test.student; #2.查看所有数据之前,先查看数据量 mysql> sele ...

  7. DQL数据查询语言——连接查询

    --内连接 两种写法 等值连接select r.*,b.bummc from t_hq_ryxx r, t_hq_bm b where r.bumbm = b.bumbm select r.*,b.b ...

  8. DQL数据查询语言

    --查询全表select * from t_hq_ryxx; --查询字段select xingm as 姓名 ,gongz as 工资 from t_hq_ryxx; --链接字段查询select ...

  9. DQL 数据查询语言 show

    2.show show databases; 查看所有的库 show tables; 查看当前库的所有的表 show tables from database; 查看指定的库下的所有表 show pr ...

随机推荐

  1. 爬虫-使用lxml解析html数据

    使用lxml之前,我们首先要会使用XPath.利用XPath,就可以将html文档当做xml文档去进行处理解析了. 一.XPath的简单使用: XPath (XML Path Language) 是一 ...

  2. 使用call、apply、bind继承及三者区别

    js里的继承方法有很多,比如:使用原型链的组合继承.es6的Class.寄生继承以及使用call.apply.bind继承.再说继承之前,我们先简单了解下它们的区别. 一.区别: 同:三者都是改变函数 ...

  3. 哈佛商学院MBA管理课程

    课程示例:向上管理 课程 什么是向上管理? 了解自己和上司 建立合作关系 与上司进行有效沟通 管理糟糕的上司 向上管理课程内容: 全部课程目录 全部为离线文件(可有偿提供) 包括课程的全部内容,视频. ...

  4. 阿里云VOD(二)

    一.准备工作 1.设置不转码 测试之前设置默认"不转码",以节省开发成本 2.找到子账户的AccessKey ID 3.给子账户添加授权 AliyunVODFullAccess 4 ...

  5. Django的数据库读写分离

    Django的数据库读写分离 1.首先是配置数据库 在settings.py文件中增加多个数据库的配置: DATABASES = { 'default': { 'ENGINE': 'django.db ...

  6. vue3.0改变概况

    一.slot API在render实现原理上的变化 二.全局API使用规范变化 三.Teleport添加 四.composition API变化 五.v-model变化

  7. 洛谷P2865

    感觉此题可作为严格次短路的模板,因此来写一写 Description 给定 \(n\) 个点,\(r\) 条双向道路,求从 \(1\) 号点到 \(n\) 号点的严格次短路 Solution 维护两个 ...

  8. 谁再把IDEA的Project比作Eclipse的Workspace,我就跟谁急

    前言 你好,我是A哥(YourBatman). 有一个观点:若一个Java开发者能把IDEA玩得666,则技术一定不会差:但若玩不转IDEA(如不会设置.定制.解决日常问题.快捷键等等),那大概率水平 ...

  9. Urlrewrite

    Urlrewrite  地址重写,用户得到的全部都是经过处理后的URL地址 过滤用户的所有请求,符合规则的便对其进行重定向 rule结点中from默认使用的正则表达式来匹配,若用户访问服务器时的URL ...

  10. vue项目中如何引用tinymce

    最近公司在做一个CMS系统的项目,其中富文本编辑框用的很多,目前流行的也很多,包括wangEditor.TinyMCE.百度ueditor.kindeditor.CKEditor等.经过自己的一番翻箱 ...