张森霖 10-15

数据查询语句:

-- 数据查询语言DQL(select)
create table exam(
    id INT(11) PRIMARY KEY AUTO_INCREMENT,
    name VARCHAR(20),
    English INT,
    Chinese INT,
    Math int
);
INSERT INTO exam VALUES(NULL,'小花',99,99,99);
INSERT INTO exam VALUES(NULL,'小蓝',89,89,89);


-- 1.全表查询: SELECT * FROM 表;
SELECT * FROM exam;
-- 2.查询部分字段: SELECT 字段,字段,字段... FROM 表;
SELECT NAME,English,Math FROM exam;
-- 3.过滤重复字段行: SELECT [DISTINCT] *|列名 FROM 表;
SELECT DISTINCT Math FROM exam;
SELECT DISTINCT name,Math FROM exam;
-- 4.查询字段起别名: SELECT 字段 AS 新字段名,字段 新字段名 FROM 表;
SELECT NAME,English AS English_score FROM exam;
-- 5.查询指定字段
SELECT NAME,English,Chinese FROM exam WHERE NAME='李白';
-- 6.使用表达式+、-、*、/
SELECT id,NAME,English-20 AS _English FROM exam;
SELECT NAME,English+Math+Chinese FROM exam;
-- 7.模糊查询
SELECT * FROM exam WHERE NAME LIKE '小_';
SELECT * FROM exam WHERE NAME LIKE '%%';
-- 8.使用and,or
SELECT * FROM exam WHERE English > 90 AND Chinese >90;
SELECT * FROM exam WHERE English < 90 or Math >99;
-- 9.使用in,not in
SELECT * FROM exam WHERE id=2 OR id=3 OR id=4;
SELECT * FROM exam where id IN(2,3,4);
SELECT * FROM exam where id not IN(2,3,4);
-- 10.使用between...and []
SELECT * FROM exam WHERE English BETWEEN 90 AND 100;
-- 11. is null,is not null
INSERT INTO exam(id,NAME) VALUES(NULL,NULL);
SELECT * FROM exam WHERE NAME IS NULL;
SELECT * FROM exam WHERE NAME IS not NULL;
-- 11.排序查询
SELECT * FROM exam ORDER BY Chinese ASC;
SELECT * FROM exam ORDER BY Chinese DESC;
SELECT * FROM exam ORDER BY English DESC,Chinese DESC;-- 如果英语成绩相同,按照汉语成绩降序排列
SELECT * FROM exam WHERE NAME LIKE '小%' ORDER BY English ASC;
-- 12.聚合函数
SELECT SUM(English+Math+Chinese) FROM exam;
SELECT COUNT(id) FROM exam WHERE NAME IS NOT NULL;
SELECT MAX(English) FROM exam;
SELECT MIN(English) FROM exam;
SELECT AVG(English) FROM exam ;

分组查询和分页查询:

-- 一、分组查询

CREATE TABLE emp(
    empno INT PRIMARY KEY AUTO_INCREMENT,
    ename VARCHAR(20),
    job VARCHAR(20),
    mgr int,
    hiredate DATE,
    sal DOUBLE(7,2),
    commit double(5,2),
    deptno INT NOT NULL 
);

INSERT INTO emp VALUES
(1002,'白展堂','clerk',1001,'1983-05-09',7000.00,200.00,10),
(1003,'李大嘴','clerk',1002,'1980-07-08',8000.00,100.00,10),
(1004,'吕秀才','clerk',1002,'1985-11-12',4000.00,null,10),
(1005,'郭芙蓉','clerk',1002,'1985-03-04',4000.00,null,10),
(2001,'胡一菲','leader',null,'1994-03-04',15000.00,NULL,20),
(2002,'陈美嘉','manger',2001,'1993-05-24',10000.00,300.00,20),
(2003,'吕子乔','clerk',2002,'1995-05-19',7300.00,100.00,20),
(2004,'张伟','clerk',2002,'1994-10-12',8000.00,500.00,20),
(2005,'曾小贤','clerk',2002,'1993-05-10',9000.00,700.00,20),
(3001,'刘梅','leader',null,'1968-08-08',13000.00,NULL,30),
(3002,'夏冬梅','manger',3001,'1968-09-21',10000.00,600.00,30),
(3003,'夏雪','clerk',3002,'1989-09-21',8000.00,300.00,30),
(3004,'张一山','clerk',3002,'1991-06-16',88000.00,200.00,30);


-- 1.查询每个部门的平均工资
SELECT deptno,AVG(sal) FROM emp GROUP BY deptno;
-- 2.查询每个职位的最高工资和最低工资
SELECT job,MAX(sal),MIN(sal) FROM emp GROUP BY job;
-- 3.查询每个部门每种职位的最高工资
SELECT deptno,job,MAX(sal) FROM emp GROUP BY deptno,job;
-- 4.查询每个部门的最高薪水,只有最高薪水大于15000的记录才被输出显示
SELECT deptno,MAX(sal)AS max_sal FROM emp GROUP BY deptno HAVING max_sal>=15000;
-- 5.查询每个部门的平均工资
SELECT deptno,AVG(sal) FROM emp GROUP BY deptno HAVING AVG(sal)>9000;
-- 6.Havaing子句与where子句的区别
(1)where是用来过滤记录的,HAVING是用来过滤分组的
(2)过滤的时机不相同,先过滤Where后过滤Having.  
(3)WHERE是在查询表时逐行过滤以选取满足条件的记录
(4)having是在数据查询后并且分完组后对分组进行过滤的
(5)HAVING必须跟在group BY
(6)查询语句执行顺序:5select 1from 2where 3group by 4having 6order by


-- 二、分页查询
-- 1.从第几页开始多少页(下标从0开始)
SELECT * FROM emp LIMIT 0,3;
-- 2.每页几条第几页==需要查看第几页-1)乘以第二个参数
SELECT * FROM emp LIMIT 10,5;-- 每页五条第三页(3-1)*5
SELECT * FROM emp LIMIT 2,2; --每页2条第2页(2-1)*2
-- 3.查看工资最高的前十个职员信息
SELECT * FROM emp ORDER BY sal DESC LIMIT 0,10;

今日总结:昨天安装的mysql登录不上,今天搞了一晚上,重装了mysql。 今天学的数据库知识还没有复习,明天抓紧时间复习。

标签

评论

this is is footer