Sql statement basic points summary

One, add

  1. Add a student information to the stu table
INSERT INTO stu VALUES(1008,'zhangsna12',12,'jining','123232465');
  1. another way
INSERT INTO stu(stuid,name,age,tel,address) VALUES(1002,'lisi',13,'31315313','jining');-- 建议用这种

INSERT INTO stu(name,age,tel,address) VALUES('1',13,'31315313','jining'),-- 建议用这种
							                ('2',13,'31315313','jining');

Two, modify

SELECT * from stu;

Be sure to add where conditions, modify multiple columns separated by commas

UPDATE stu SET name = '李文英',address='山东' WHERE stuid=1001;

Three, delete

DELETE FROM stu WHERE stuid=1008;

Four, single table query

1. Simple query

-- 1.查询所有学生信息
SELECT *FROM student;
-- 1.查询学生信息,显示学生姓名,电话,性别
SELECT name,phone,sex FROM student;-- name为关键字,所以要加引号区分
-- 3.查询哪些班级有学生,显示班级编号(去重:DISTINCT)
SELECT DISTINCT cid FROM student;
-- 4.使用算术运算符,as关键字用于重新命名,可加可不加
SELECT socre as 加分前的成绩, socre+10 加分后的成绩 FROM score;

Exercise 1

-- 1.查询所有员工信息
SELECT *FROM employee;
-- 1.查询所有员工的姓名及对应工资
SELECT ename,salary FROM employee;
-- 3.过滤员工表中的重复数据
SELECT DISTINCT *FROM employee;
-- 4.查询出每位员工的工资,并加一
SELECT salary , salary + 1 FROM employee;
-- 5.使用别名表示姓名,职位,电话,工资
SELECT ename 姓名,phone 电话,salary 工资 FROM employee;

2.WHERE

-- 1.查询学号为4的学生信息
SELECT * FROM student WHERE stuid=4;
-- 2.查询考试成绩满足给定条件的学生学号,分数
SELECT stuid,socre FROM score WHERE socre>=60 AND socre<=80;
SELECT stuid,socre FROM score WHERE socre BETWEEN 60 AND 80;
SELECT stuid,socre FROM score WHERE socre=69 OR socre=79;
SELECT stuid,socre FROM score WHERE socre in(69,79);

3.NOT NOT

% Represents multiple characters

SELECT stuid,socre FROM score WHERE socre NOT BETWEEN 60 AND 80;

4.LIKE like

SELECT * FROM student WHERE name LIKE '张%';
SELECT * FROM student WHERE name LIKE '%三';
SELECT * FROM student WHERE name LIKE '%m%';

_ Represents a single character

SELECT * FROM student WHERE name LIKE '张_';
SELECT * FROM student WHERE name LIKE '张__';

5.is NULL

SELECT * FROM score WHERE socre IS NULL;
SELECT * FROM score WHERE socre IS NOT NULL;

Exercise 2

SELECT * FROM employee WHERE salary>=5000;
SELECT * FROM employee WHERE salary>=3000 AND salary<=5000;
SELECT * FROM employee WHERE salary in (5000,3000,8000);
SELECT * FROM employee WHERE hiredate BETWEEN '2016-02-01' AND '2016-03-01';
SELECT * FROM employee WHERE manager is NULL;
SELECT ename,job,phone FROM employee WHERE ename LIKE '王%';

6. Aggregate functions

  1. avg(): find the average
-- 求 score 成绩表中科目编号为1的所有学生平均成绩
SELECT AVG(socre) 平均成绩 FROM score WHERE subid=1;
  1. COUNT(): Find how many records
-- 用*表示所有列
SELECT COUNT(*) FROM score WHERE subid=1;
  1. SUM(): sum
-- 求score成绩表中所有学生成绩的总和
SELECT SUM(socre) FROM score;
  1. MAX() and MIN(): find the best value
SELECT MAX(socre) 最高分,MIN(socre)最低分 FROM score WHERE subid=2;

Find the average score of each subject

Do not use grouping

SELECT DISTINCT subid FROM score;
SELECT AVG(socre) FROM score WHERE subid=1;
SELECT AVG(socre) FROM score WHERE subid=2;
SELECT AVG(socre) FROM score WHERE subid=3;

  1. GROUP BY statement

Use grouping (with each, each, or with aggregate function)

-- mysql中可单独使用,分组后的列可以不出现在select中
SELECT AVG(socre) FROM score GROUP BY subid;
-- SQLServer中不可单独使用,需要结合having语句,而且分组后的列必须出现在select中
SELECT AVG(socre),subid FROM score GROUP BY subid;

After checking all the scores plus 5 points, the score is still less than 60 points

HAVING for query results

SELECT stuid,socre+5 加分后的成绩 FROM score HAVING 加分后的成绩 <60;

WHERE can only filter for columns that really exist in the table

SELECT stuid, socre FROM score WHERE socre <60;
-- 平均分大于70的科目
SELECT subid,AVG(socre) FROM score GROUP BY subid HAVING AVG(socre)>70;

7. Sorting

  1. ORDER BY statement
  • asc ascending (default)
  • desc descending
SELECT * FROM student;
SELECT * FROM student ORDER BY cid,birthday ASC;
SELECT * FROM student ORDER BY phone DESC;
  1. LIMIT statement, used to page
    the coordinates of the LIMIT data, starting from 0, the number of items displayed
SELECT * FROM student ORDER BY birthday ASC LIMIT 0,5;

Exercise 3

SELECT * FROM employee ORDER BY salary ASC LIMIT 0,3;
SELECT * FROM employee ORDER BY did DESC, hiredate ASC, salary DESC;
SELECT * FROM employee ORDER BY salary DESC LIMIT 2,3;

Five, multi-table query

Cross connect

SELECT * FROM student, classinfo;

1. Equivalent link

-- 查询李四所在班级,显示李四所在班级编号,姓名,班级名
-- 1.=链接两个表
SELECT student.cid,name,cname FROM student, classinfo
-- 2.确定链接条件
where student.cid=classinfo.cid
-- 3.确定其他筛选条件
and name= '李四';
-- 查询张三c++考试成绩,显示学生姓名,班级名,学号,成绩,科目名
SELECT `name`,cname,student.stuid,socre,subname
FROM student, classinfo, `subject`,score
WHERE student.cid=classinfo.cid
AND `subject`.subid=score.subid
AND student.stuid=score.stuid
AND student.`name`= '张三'
AND subname = 'c++';

2. Internal link

-- 查询李四所考科目的平均成绩,显示姓名,平均成绩
-- 1.把两个表连成大表
SELECT `name`,AVG(socre) FROM student INNER JOIN score
-- 2.链接条件
ON student.stuid=score.stuid
-- 3.筛选条件
WHERE `name`='李四';
-- 查询张三c++考试成绩,显示学生姓名,班级名,学号,成绩,科目名
SELECT `name`,cname,student.stuid,socre,subname
FROM student INNER JOIN classinfo
ON student.cid=classinfo.cid
INNER JOIN score
ON student.stuid=score.stuid
INNER JOIN `subject`
ON `subject`.subid=score.subid
WHERE student.`name`= '张三' AND subname = 'c++';

3. External connection

1. Left outer join: (It is recommended to use the left outer join instead of the right outer join). The left table is the main one, the matching data is found in the right table. If there is no matching data, it is filled with null, and the data in the left table must be queried

The inner join and equivalent link are the intersection of the left and right tables. If the match is successful, it will be queried, otherwise it will not be displayed

-- 查询所有学生课程的考试成绩,
-- 查询结果保留学生ID、姓名、性别、课程ID、成绩
SELECT student.stuid,`name`,sex,subid,socre FROM student LEFT JOIN score
ON student.stuid=score.stuid;

2. Right outer join: mainly on the right table, find matching data in the left table, fill it with null if not, the data in the left table must be queried

-- 而内连接和等值链接是左右两个表的交集,如果匹配成功则查询出来,否则不显示
SELECT student.stuid,`name`,sex,subid,socre FROM student RIGHT JOIN score
ON student.stuid=score.stuid;

Guess you like

Origin blog.csdn.net/qq_43405938/article/details/108692907