标签:dql 外连接 中国人 查看 城市 ati join sql文件 数据量
#1.查询表中所有的数据
mysql> select * from test.student;
#2.查看所有数据之前,先查看数据量
mysql> select count(*) from test.student;
#3.查询指定列
mysql> select user,host from mysql.user;
#4.按条件查询
mysql> select * from test.student where id=‘8‘;
mysql> select id,name from test.student where id=‘8‘;
#上传sql文件到服务器
[root@db01 ~]# rz world.sql
#导入sql到数据库
mysql> source /root/world.sql;
mysql> \. /root/world.sql
#1.查看库下面的表
mysql> show tables from world;
mysql> use world
mysql> show tables;
+-----------------+
| Tables_in_world |
+-----------------+
| city |
| country |
| countrylanguage |
+-----------------+
3 rows in set (0.00 sec)
#2.查看表结构
mysql> desc city;
#3.查询所有数据
mysql> select count (*) from city;
mysql> select * from city;
#4.查询指定列数据
mysql> select name,population from city;
#5.按照人口数量排序
#升序
mysql> select name,population from city order by population;
#降序
mysql> select name,population from city order by population desc;
#6.查看人口数量最多排名前十的城市
mysql> select name,population from city order by population desc limit 10;
#7.按照步长查询数据
#查询数据从10后面开始计算,展示20条数据,20就是步长
mysql> select id,name,population from city limit 10,20;
mysql> select id,name,population from city limit 0,60;
mysql> select id,name,population from city limit 60,60;
mysql> select id,name,population from city limit 120,60;
#1.条件查询where的符号
where的条件符号: = < > >= <= != <>
where的连接符:and or like in
#2.查看中国城市的人口数量
mysql> select CountryCode,name,population from city where CountryCode=‘CHN‘;
#3.查看黑龙江省城市的人口数量
mysql> select CountryCode,District,name,population from city where CountryCode=‘CHN‘ and District=‘heilongjiang‘;
#4.查询中国人口数量小于10万的城市
mysql> select CountryCode,population,name from city where CountryCode=‘CHN‘ and population<‘100000‘;
#5.查看国家代码以H开头的
mysql> select * from city where CountryCode like ‘H%‘;
#6.查看国家代码以H结尾的
mysql> select * from city where CountryCode like ‘%H‘;
#7.查看国家代码包含H的
mysql> select * from city where CountryCode like ‘%H%‘;
#8.查询中国城市和美国城市的人口数量
mysql> select CountryCode,name,population from city where CountryCode=‘CHN‘ or CountryCode=‘USA‘;
mysql> select CountryCode,name,population from city where CountryCode in (‘CHN‘,‘USA‘);
#9.联合查询
mysql> select CountryCode,name,population from city where CountryCode=‘CHN‘ union all select CountryCode,name,population from city where CountryCode=‘USA‘;
#查询世界上小于100人的城市是哪个国家的?
#1.审题:查看需要查询哪些数据
城市名字 城市人口数量 国家名字
#2.找到查询内容的字段在哪个表
城市名字 城市人口数量 国家名字
city.name city.population country.name
#3.找出两个表中关联的列
city.countrycode
country.code
#4.编写语句
select city.name,city.population,country.name from city,country where city.countrycode=country.code and city.population < ‘100‘;
select city.name,city.population,country.name from city natural join country where city.population < ‘100‘;
#查询世界上小于100人的城市是哪个国家的,使用什么语言?
#1.审题:查看需要查询哪些数据
城市名字 城市人口数量 国家名字 国家的语言
#2.找到查询内容的字段在哪个表
城市名字 城市人口数量 国家名字 国家的语言
city.name city.population country.name countrylanguage.language
#3.找出三个表相关联的列
city.countrycode
country.code
countrylanguage.CountryCode
#4.编写语句
select city.name,city.population,country.name,countrylanguage.language from city,country,countrylanguage where city.countrycode=country.code and country.code=countrylanguage.CountryCode and city.population < ‘100‘;
#查询人口数量大于100万的城市,列出他们的国家代码和国家语言
1.传统连接:
select city.name,city.population,countrylanguage.CountryCode,countrylanguage.language from city,countrylanguage where countrylanguage.CountryCode=city.CountryCode and city.population > ‘1000000‘;
2.自连接:
select city.name,city.population,countrylanguage.CountryCode,countrylanguage.language from city natural join countrylanguage where city.population > ‘1000000‘;
#注意:
1.自连接会自动去获取两个表之间的关联列和数据,所以自连接的两个表必须有相同的字段和数据
select * from 表1 join 表2 on 关联条件 where 条件
#注意:
表 1 是小表
表 2 是大表
#查询世界上小于100人的城市是哪个国家的,国家代码是什么
1.传统链接:
select city.population,city.name,country.name,country.code from city,country where country.code=city.countrycode and city.population < ‘100‘;
2.内连接:
select city.population,city.name,country.name,country.code from country join city on country.code=city.countrycode where city.population < ‘100‘;
#查询世界上小于100人的城市是哪个国家的,用什么语言?
select city.population,city.name,country.name,countrylanguage.language from country join city on city.countrycode=country.code join countrylanguage on country.code=countrylanguage.countrycode where city.population < ‘100‘;
select city.name,city.countrycode,country.name
from city left join country
on city.countrycode=country.code
and city.population<100;
select city.name,city.countrycode,country.name
from city right join country
on city.countrycode=country.code
and city.population<100;
#范围查询OR语句
mysql> select * from city where countrycode=‘CHN‘ or countrycode=‘USA‘;
#范围查询IN语句
mysql> select * from city where countrycode in (‘CHN‘,‘USA‘);
#替换为:
mysql> select * from city where countrycode=‘CHN‘
union all
select * from city where countrycode=‘USA‘ limit 10;
标签:dql 外连接 中国人 查看 城市 ati join sql文件 数据量
原文地址:https://www.cnblogs.com/chenlifan/p/13907246.html