DQL language
DQL data query language
SELECT syntax
Query all student information (all columns, low efficiency)
select * from student;
Query specified column (student number, name) is not case sensitive
SELECT studentno,studentname FROM student;
Take the alias keyword as, you can also use spaces
SELECT studentno AS 学号,studentname AS 姓名 FROM student;
- You can also use as to alias the table, which is used to distinguish between multiple table queries
SELECT studentno AS 学号,studentname AS 姓名 FROM student AS s;
- distinct removes duplicates, the default is all
- Expressions can be used in select queries
Query that meets the conditions
select studentno,studentresult from result
where studentresult between 95 and 100;
Comparison operator
-
Wildcard used for like joining:% (0 to any number of characters) _ (one character)
select StudentNo,StudentName from student where studentname like '李%';
-
The escape symbol \ can I use my own escape symbol
-
in matches the same character in multiple values
-
null can not be used =, and null must be written is or is not
- Note: If the default is non-empty, after deleting the content, it is not null, but ='' to match
Link query
-
Inner join.
inner join
Query the intersection of the result sets in the two tables -
SELECT s.`StudentNo`,`StudentName`,`SubjectNo`,`StudentResult` FROM `student` s INNER JOIN `result` r ON r.`StudentNo`=s.`StudentNo`
-
Outer join
outer join
-
Left outer connector
left join
to the left table as a reference, the right table one by one, the matching is not on, the process returns records left table, right table to null fill -
Right outer connection
right join
is opposite to above
-
-
Self-connection
CREATE TABLE categorg(
category_id INT(11) AUTO_INCREMENT PRIMARY KEY,
category_name VARCHAR(10),
pid INT(10)
);
INSERT INTO `categorg`(`category_name`,pid)
VALUES('软件开发',0),('美术设计',0),
('数据库继承',1),('ps基础',2),('色彩搭配',2),
('php基础',1),('一起学java',1);
select
a.`category_name` as '父栏目',
b.`category_name` as '子栏目'
from
`categorg` a
join `categorg` b
on b.`pid`=a.`category_id`
where a.pid = 0 ;
- Non-equivalent join
等值连接
SELECT s.`StudentNo`,`StudentName`,`SubjectNo`,`StudentResult`
FROM `student` s,`result` r
WHERE r.`StudentNo`=s.`StudentNo`;
Little knowledge
round() rounds
rand() random numbers
Use in for subquery
SELECT * FROM student WHERE `StudentNo` IN (SELECT ROUND(RAND()*2+1011));
Cartesian collection, very memory consuming, not recommended