目录
- 基础查询语句
- 条件查询语句
基础查询语句
#语法:select 查询列表 from 表名;
#查询列表可以是:字段、表达式、常量、函数等,可以有多个部分组成,中间用逗号隔开
#例如:select 字段1,字段2,表达式 from 表;
#执行顺序:1.from子句 2.select子句
#(1)查询常量
select 100; #形成一个虚拟表格
#(2)查询表达式
select 100+100;
select 100%3;
#(3)查询单个字段
select name from world.city;
#(4)查询多个字段
select id,name from world.city;
#(5)查询所有字段
select * from world.city;
#(6)查询函数
select now(); #显示当前时间
select user(); #显示当前登陆的用户名和ip
select version(); #显示mysql版本
select database(); #显示当前所选择的数据库
#(7)为字段起“别名”
#i.使用AS关键词
select city.name as '城市' from city;
#ii.使用空格
select city.name '城市' from city;
#混用
select name 城市,population as '人口' from city;
#(8)去重:去除重复数据distinct
-- select CountryCode from city;
select distinct CountryCode from city;
条件查询语句
#语法:select 查询列表 from 表名 where 筛选条件;
#执行顺序:1)from子句 2)where子句 3)select子句
#(1)按关系表达式筛选(一个筛选条件)
#查询国家语言表中,国家编码为中国的(CHN)
select CountryCode,Language from world.countrylanguage where countrycode = 'CHN';
#不等于的符号:<>或!=
select CountryCode,Language from world.countrylanguage where countrycode != 'CHN';
select CountryCode,Language from world.countrylanguage where countrycode <> 'CHN';
#查询人口大于10亿(1000000000)的国家
select name,population from world.country where population > 1000000000;
#查询GNP增长超过10%的国家和地区
select Name,gnp,gnpold,gnp/gnpold from world.country where gnp/gnpold >=1.1;
#(2)按逻辑表达式筛选(多个筛选条件)
#查询国家表,预期寿命小于40岁和大于80岁的国家和地区
-- 写法1:
select name,LifeExpectancy from world.country where LifeExpectancy < 40 or LifeExpectancy > 80;
-- 写法2:
select name,LifeExpectancy from world.country where not(LifeExpectancy >= 40 and LifeExpectancy <= 80);
#查询城市表,找出中国人口超过5百万(50000000)的城市
select CountryCode,name,Population from world.city where Population > 5000000 and CountryCode = 'CHN';
#查询国家表,预期寿命大于80岁,或国土面积超过100万(1000000)平方公里并且人口大于1亿(100000000)
select name,lifeexpectancy,surfacearea,population from world.country where LifeExpectancy >80 or (SurfaceArea > 1000000 and population >100000000);
#(3)模糊查询 (like/not like)
#功能:一般和通配符搭配使用,对字符型数据进行部分匹配查询
#常见的通配符:_ 任意单个字符 % 任意多个字符,支持0-多个
#查询国家名称中包含字母INA的
select name from world.country where name like '%INA%';
#查询最后一个字母为IA的国家
select name from world.country where name like '%IA';
#查询第一个字母为CH的国家
select name from world.country where name like 'CH%';
#查询第三个字母为IN的国家
select name from world.country where name like '__IN%';
#查询国家信息表,第三个字母为_的记录,转义字符\_
select name from world.country where name like '__\_%';
-- 指定用'/'符号来说明跟在其后面的通配符。其他符号也可以,# $ escape
select name from world.country where name like '__#_%' escape '#';
#(4)关键字 between… and…
#查询国家表,预期寿命80岁到85岁的国家或地区
-- 写法1:
select name,LifeExpectancy from world.country where LifeExpectancy >= 80 and LifeExpectancy <= 85;
-- 写法2:
select name,LifeExpectancy from world.country where LifeExpectancy between 80 and 85;
#查询国家表,预期寿命不在40岁到85岁的国家或地区
select name,LifeExpectancy from world.country where LifeExpectancy not between 80 and 85;
#(5)关键字in集合查询
#查询国家表,建国时间为1971,1981,1991年的国家
-- 写法1:
select name,IndepYear from world.country where IndepYear=1971 or IndepYear=1981 or IndepYear=1991;
-- 写法2:
select name,IndepYear from world.country where IndepYear in (1971,1981,1991);
#查询国家表,建国时间不是1971,1981,1991年的国家
select name,IndepYear from world.country where IndepYear not in (1971,1981,1991);
#(6)关键字is null(判断某个字段是否为null 不能用等号 用is)
#查询国家表,建国时间为空(null)的国家和地区alter
select name,IndepYear from world.country where IndepYear is null;
#查询国家表,比较所属国家首脑为''和null的效果alter
select name,HeadOfState from world.country where HeadOfState = ''; -- 等号只能用比较普通数值
select name,HeadOfState from world.country where HeadOfState is null; -- is只能比较null值
-- 设置字符串的" "空和类型上的null不一样,null更像是一个占位,什么数据也没有;
select name,HeadOfState from world.country where HeadOfState <=> ''; -- 安全等号,既能判断数值空也能判断null
select name,HeadOfState from world.country where HeadOfState <=> null;