基础查询

-- 1、查询name和age两列
select name,age FROM stu;
-- 2、查询所有数据 查询所有数据的话 列名的列表可以用*代替(不推荐,一般在企业开发不让用*)
select * from stu;
select id,name,age,sex,address,math,english,hiredate FROM stu;
-- 3、去除重复记录(DISTINCT)
select DISTINCT address FROM stu;
-- 4、为列名取别名 (AS) AS也可以省略不写 但是原始的之字段名必须与别名隔开至少一个空格
SELECT name as 姓名,math as 数学成绩, english as 英语成绩 FROM stu;
条件查询

select id,name,age,sex,address,math,english,hiredate FROM stu where age > 20;
select id,name,age,sex,address,math,english,hiredate FROM stu where age >= 20;
select id,name,age,sex,address,math,english,hiredate FROM stu where age >= 20 && age <= 30;
select id,name,age,sex,address,math,english,hiredate FROM stu where age >= 20 and age <= 30;
select id,name,age,sex,address,math,english,hiredate FROM stu where age BETWEEN 20 and 30;
select id,name,age,sex,address,math,english,hiredate FROM stu where hiredate BETWEEN '1998-09-01' AND '1999-09-01';
select id,name,age,sex,address,math,english,hiredate FROM stu WHERE age = 18;
select id,name,age,sex,address,math,english,hiredate FROM stu where age != 18;
select id,name,age,sex,address,math,english,hiredate FROM stu where age <> 18;
select id,name,age,sex,address,math,english,hiredate FROM stu where age = 18 || age = 20 || age = 22;
select id,name,age,sex,address,math,english,hiredate FROM stu where age = 18 OR age = 20 OR age = 22;
select id,name,age,sex,address,math,english,hiredate FROM stu where age IN(18,20,22);
select id,name,age,sex,address,math,english,hiredate FROM stu where english is null;
select id,name,age,sex,address,math,english,hiredate FROM stu where english is NOT null;
模糊查询 LIKE
select id,name,age,sex,address,math,english,hiredate FROM stu where name like '马%';
select id,name,age,sex,address,math,english,hiredate FROM stu where name like '_花%';
select id,name,age,sex,address,math,english,hiredate FROM stu where name like '%德%';
排序查询

SELECT id,name,age,sex,address,math,english,hiredate from stu ORDER BY age ASC;
SELECT id,name,age,sex,address,math,english,hiredate from stu ORDER BY math DESC;
SELECT id,name,age,sex,address,math,english,hiredate from stu ORDER BY math DESC,english asc;
聚合函数 (null值不参与所有的聚合函数运算)

select count(id)FROM stu;
select count(*)from stu;
SELECT MAX(math) FROM stu;
select min(math) from stu;
select sum(math) from stu;
select AVG(math) from stu;
分组查询

SELECT sex,avg(math) FROM stu GROUP BY sex;
select sex,count(*),avg(math) FROM stu GROUP BY sex;
select sex,count(*),avg(math) FROM stu where math > 70 GROUP BY sex;
select sex,count(*),avg(math) FROM stu where math > 70 GROUP BY sex HAVING count(*) > 2;
分页查询

SELECT * FROM stu limit 0,3;
SELECT * FROM stu limit 0,3;
SELECT * FROM stu limit 3,3;
SELECT * FROM stu LIMIT 6,3;