Mysql
数据库入门—数据查询
Author:qyan.li
Date:.2.21
文章目录
``Mysql``数据库入门---数据查询1. ``select``关键字2.条件查询3.排序查询1.select
关键字
select
意为选择之意,可以借助于select
关键字完成数据库内容的查询
借助于关键字
查询
基本用法:select 查询列表 from 表名
# 数据查询select 100; # 查询变量100select 1000 + 100; # 查询表达式# 数据库字段查询select owner from student_information.pet # 查询单个字段slelect owner,name from student_information.pet # 查询多个字段select * from student_information.pet # 查询所有字段
借助于查询函数
查询
查询函数可以辅助用户完成数据库、用户基本情况的查询
select now(); # 显示当前时间select user(); # 用户名和ipselect version(); # 显示Mysql版本select database(); # 显示数据库
小Tips:
字段别名:
select name as 姓名 from student_information.pet;
上述语句表明为name
起别名为姓名
此处的
as
可以简略,直接表示为select name 姓名 from student_information.pet;
别名可以与其他
sql
结合形成更加复杂的语句
select now() as 当前时间
select name 宠物名,owner as 主人 from student_information.pet;
去除重复数据:
数据库应用中,某些时刻会需要提炼出某字段中所有不相重复的数据,此时需要distinct
关键字tinct
select name from student_information.pet;select distinct name from student_information.pet;
2.条件查询
数据库查询过程中,经常会遇到筛选出满足特定条件的数据,此时会需要where
关键字,后加筛选条件
完成数据的查询,一般形式为select 字段名 from 数据表名称 where 筛选条件
Mysql
运算符:
Mysql
中运算符一般包括 1. 算术运算符 2.比较运算符 3. 逻辑运算符 4.位运算符
算术运算符:
加减乘除(除法借助于/或者div表示)
、取余(%或者mod表示)
比较运算符:
比较运算符:
小于、大于、小于等于、大于等于较为常规
不等于(借助于<>或者!=表示)
为空或者不为空(is null 或者 is not null)
在两值之间(between a and b)
是否在集合中(in 或者 not in)
是否模糊匹配(like或者not like)
正则表达式匹配regexp
逻辑运算符:
与或非(and(&&) or(||) not(!))
异或(xor)
位运算符:
应用并不是非常广泛,不做讨论
简单示例:
select owner from student_information.pet where name = 'liqiyan';select gnp/gnpold,name from country where gnp/gnpold > 1.1;select * from student_information.pet where not(name = 'liqiyan' and sex = 'm');
运算符筛选查询:
运算符筛选种类、组合多样,可根据需要自行组合,此处仅介绍重难点筛选方法
模糊匹配查询
模糊匹配查询一般借助于like(not like)和通配符
进行实现
通配符主要包含两种_表示任意单个字符
,%表示任意多个字符
简单示例:
# 查询国家名称中包含字母INAselect * from world.country where name like '&INA%'# 查询国家名称中最后一个字母为IAselect name from world.country where name like '%IA'# 查询国家名称中第一个字母为CHselect * from world.country where name like 'CH%'# 查询国家名称中第三个字母为_的记录(借助于转义字符完成)select name from world.country where name like '__\_%'# 转义字符一般借助于\进行表示,但是该字符可以自定义,借助于escape关键字select name from world.country where name like '__#_%' escape '#';
关键字查询
此处提及的关键字,主要包含上述运算符介绍中提及的between and 和 is null 和 in(not in)
等等
between and
关键字
# 查询预期寿命在80-85岁之间的国家和地区select name,LifeExpectancy from world.country where LifeExpectancy >= 80 and LifeExpectancy <= 85;select name,LifeExpectancy from world.country where LifeExpectancy between 80 and 85;
集合查询in(not in)
关键字
# 查询独立年份在1971,1981和1991年的国家select name,IndepYear from world.country where IndepYear = 1971 or IndepYear = 1981 or IndepYear = 1991;select name,IndepYear from world.country where IndepYear in (1971,981,1991);
判断是否为空(is null或者is not null)
select * from world.country where IndepYear is null;
此处需要提及
null
和空字符串的区别:二者是不等价的两个概念,null更像是一个占位,表示什么都没有,而空字符串表示此处包含有内容,只不过内容为空而已。
select name,HeadOfState from world.country where HeadOfState = '';select name,HeadOfState from world.country where HeadOfState is null;
安全等号
(<=>不必区分is和普通等号的区别,即在此处空字符串和null可以混用)
select name,HeadOfState from world.country where HeadOfState <=> '';select name,HeadOfState from world.country where HeadOfState <=> null;
3.排序查询
在数据库查询操作中,经常会遇到数据需要按照一定的规则进行排序,此时会应用到sql
语句中的排序查询关键字order by
,结合asc和desc
可以实现降序、升序
的排列,一般语法规则为select <查询列表> from <数据表名称> where <筛选条件> order by 排序列表
在排序中,存在
asc和desc
两种排序规则,分别为升序和降序,默认情况下为降序
简单示例:
# 按照人口进行降序排列,显示国家名称select name,population from world.country order by population desc;# 按照GDP增长的比例升序排列select name,GNP,GNPOld,GNP/GNPOld GNP增长率 from world.country order by GNP增长率 asc;
小Tips:
结合函数可以实现更高级别的排序
select name,length(name) as 字符个数 from world.country order by 字符个数;
排序过程中可以限制多条件完成排序
select name,length(name) as 字符个数 from world.country order by 字符个数,Population desc;
排序可以直接指定列号进行排序
select * from world.country order by 2 desc;