MySQL
1.基本查询
语法1:select 列1,列2,列3... from 表名 where 条件
语法2:select * from 表名 where 条件 * 代表所有的列
语法3:select * from 表名 where not 列名 =某值
select* from 表名 where 列名!=某值
select * from 表名 where 列名<>某值,
语法4:select 列名 from 表名 where 列名 between 某值 and 某值
语法5:select*from 表名 where 条件1 and 条件2
语法6:select * from 表名 where 列名 ='某值' or 列名 ='某值’
select * from 表名 where 列名 in('某值''某值)
示例:
1)查询所有的学生
select * from students;
2)查所有学生的姓名和专业
select name,major from students;
3)查询所有学生的姓名专业,并取别名(注:as 用来为列取别名,可以省略)
select name as '姓名',major as '专业' from students;
4)查询年龄不是20岁的学生信息
select * from students where not age = 20;
select * from students where age != 20;
select * from students where age <> 20;
5)查询专业是"计算机科学" 或"软件工程"的学生信息
select * from students where major = '计算机科学' or major = '软件工程';
select * from students where major in ('计算机科学','软件工程');
6)查询年龄在20到22岁之间的学生姓名和信息
select name,age from students where age between 20 and 22;
7)查询入学年份是2021或2022的学生信息
select * from students where year = 2021 or year = 2022;
select * from students where year in (2021,2022);
8)查询成绩大于85分且专业不是"电子工程"的学生信息
select * from students where score > 85 and major != '电子工程';
2.模糊查询
语法:select * from 表名 where 列名 like '通配符'
常用的两个通配符:
%表示任意多个字符
_表示任意单个字符
示例:
1)查询姓“王"的学生信息
select * from students where name like '王%';
2)查询住址包含"区"字的学生信息
select * from students where address like '%区%';
3)查询专业以"工程"结尾的学生信息
select * from students where major like '%工程';
4)查询姓名第二个字是"三"得学生信息
select * from students where name like '_三%';
5)查询地址不以"北京市"开头的学生信息
select * from students where address not like '北京市%';
3.排序查询
语法:select * from 表名 where 条件 order by 列 asc/desc 升序/降序 (注:默认为升序,所以asc可以省略)
示例:
1)查询所有学生的姓名和成绩,并按成绩从高到低排序;
select * from students order by score desc;
2)查询计算机科学专业的学生信息,按年龄从小到大排序
3)查询2021年入学的学生信息,先按专业升序排列,再按成绩降序排列
select * from students where year = 2021 order by major asc,score desc;
4)查询所有女生的信息,按入学年份降序排列,若入学年份相同则按年龄升序排列
select * from students where sex = '女' order by year desc, age;
4.连接查询
语法1:from <表名1> [别名1] inner join <表名2> [别名2] on <连接条件表达式>
语法2:from <表名1> [别名1],<表名2> [别名2][...] where <连接条件表达式>
语法3:distinct 去重
limit 限制查询结果数量
count() 统计数值
svg() 计算平均值
group by 对查询进行分组
having 对分组后产生的数据进行筛选
is not null 判断列值是否为空
示例:
1)查询student的八条信息(limit 限制查询结果数量)
select * from student limit 8;
2)查询course中课程名称(distinct 去重)
select distinct 课程名称 from course;
3)查询sc中有成绩的学生学号和课程号(is not null 判断列值是否为空)
select id,course from sc where score is not null;
4)统计sc中选修了课程的学生人数(count() 统计数值)
select count(distinct sno) from sc;
5)计算sc中07003号课程学生的平均成绩(svg() 计算平均值)
select svg(score) from sc where course = '07003';
6)统计党团员的男女人数(group by 对查询进行分组)
select 政治面貌,sex,count() : from student group by 政治面貌,sex;
7)查询选修了两门以上课程的学生号(having 对分组后产生的数据进行筛选)
select id,count(),avg(score) from sc group by id having count(*) >= 2;
8)连接student和sc表
select * from student ,sc where student.sno = sc.sno;
select * from student inner join sc on student.sno = sc.sno;
9)联表查询 country 与 city ,查询国家政体为共和国且国家人口在一千万以上的城市的名称和城市人口、所属国家
select city.population,country.name from country inner join city on city.countryCode = country.code
where country.governmentform = 'Republic' and city.population > 10000000;
10)联表查询 city 与 country ,查询有超过两条城市记录的国家名称,按国家分组并筛选出城市数量大于2的国家。
select country.name as '国家名称' from city inner join country on city.countryCode = country.code group by city.countrycode having count(city.name) > 2;
11)查询欧洲国民生产总值GNP排名前20的国家名称及GNP值
select name as '国家名称',GNP as 'GNP值' from country where continent = 'Europe' order by GNP desc limit 20;

浙公网安备 33010602011771号