DQL 数据查询语言
查询数据(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 数据查询语言的更多相关文章
- 八:SQL之DQL数据查询语言单表操作
前言: DQL数据库查询语言是我们在开发中最常使用的SQL,这一章总结了单表操作部分的常用查询方式 主要操作有:查询所有字段.查询指定字段.查询指定记录.带IN的关键字查询,范围查询,陪查询.查询空值 ...
- 九:SQL之DQL数据查询语言多表操作
前言: 一:数据准备 员工表emp 和部门表 dept 注意:我在录入员工表的时候,特意添加了两条没有部门的员工,他们的部门id对应为null; --分别创建部门和员工表,并实现一对多关系 DROP ...
- DQL 数据查询语言 IS (information_schema)
3.information_schema 统计信息库 1.介绍: 视图 1.安全: 只允许查询,不知道操作的对象是谁. 2.方便: 只需要简单的select语句即可使用. 2.作用: 1.方便我们做数 ...
- DQL 数据查询语言 select
1.select 1.select 单独使用 (1) 查询数据库的参数 查看端口: select @@port; 查看数据路径 select @@datadir; (2)调用内置函数 查看当前库 se ...
- MySQL数据库之DQL(数据查询语言)
1.MySQL之DQL查询AS CONCAT LIKE的使用 (1)select 列名1,列名2,...... from 表名 [where 条件] 查询所有字段用*,不带where条件的话,就会把表 ...
- 第六章 DQL 数据查询语言
一.select 简单查询命令 #1.查询表中所有的数据 mysql> select * from test.student; #2.查看所有数据之前,先查看数据量 mysql> sele ...
- DQL数据查询语言——连接查询
--内连接 两种写法 等值连接select r.*,b.bummc from t_hq_ryxx r, t_hq_bm b where r.bumbm = b.bumbm select r.*,b.b ...
- DQL数据查询语言
--查询全表select * from t_hq_ryxx; --查询字段select xingm as 姓名 ,gongz as 工资 from t_hq_ryxx; --链接字段查询select ...
- DQL 数据查询语言 show
2.show show databases; 查看所有的库 show tables; 查看当前库的所有的表 show tables from database; 查看指定的库下的所有表 show pr ...
随机推荐
- leetcode 886. 可能的二分法(DFS,染色,种类并查集)
题目链接 886. 可能的二分法 题意: 给定一组 N 人(编号为 1, 2, ..., N), 我们想把每个人分进任意大小的两组. 每个人都可能不喜欢其他人,那么他们不应该属于同一组. 形式上,如果 ...
- [usaco2010 Oct]Soda Machine
题目描述 有N个人要去膜拜JZ,他们不知道JZ会出现在哪里,因此每个人有一个活动范围,只要JZ出现在这个范围内就能被膜拜, 伟大的JZ当然希望膜拜他的人越多越好,但是JZ不能分身,因此只能选择一个位置 ...
- Gradle使用及配置
构建工具:Gradle(6.8) 下载地址:https://gradle.org/releases/ 下载最新版的二进制文件即可,下载"gradle-6.8.1-bin.zip文件,下载后完 ...
- linux中的虚拟环境工具
1.虚拟环境工具的学习 python的虚拟环境,其实就是在机器上,方便的创建出多个解释器,每个解释器运行一个项目,互相之间不受影响 2.virtualenv工具,可以方便的创建,使用,删除也很方便 3 ...
- Base64原理 bits 3->4 8bits/byte-->6bits/byte
实践: window.btoa('a')a YQ==abcdef YWJjZGVmabc YWJjab YWI= https://en.wikipedia.org/wiki/Base64 The Ba ...
- 布隆过滤器 数据同步业务 :google-guava+spring-boot-api+mybatis, 缺失值全匹配查找
布隆过滤器 数据同步业务 :google-guava+spring-boot-api+mybatis, 缺失值全匹配查找
- (Sql Server)存储过程(转载)
SQL Server 存储过程 Transact-SQL中的存储过程,非常类似于Java语言中的方法,它可以重复调用.当存储过程执行一次后,可以将语句缓存中,这样下次执行的时候直接使用缓存中的语句.这 ...
- P2573 [SCOI2012]滑雪 题解
下午花了三个小时肝这道题,心态差点爆炸! 下面是分析: 1 题目要求: 2 求最小生成树 3 但是 4 - a是从1号点开始的 --> 如果以后的某个点比一号高,则不可能到达 5 - a只能从高 ...
- loj10087
Southwestern Europe 2002,题面可参考 POJ 1201. 给定 n 个闭区间 [a_i,b_i] 和 n 个整数c_i .你需要构造一个整数集合Z ,使得对于任意i (1< ...
- Spark:常用transformation及action,spark算子详解
常用transformation及action介绍,spark算子详解 一.常用transformation介绍 1.1 transformation操作实例 二.常用action介绍 2.1 act ...