DQL language (1)

DQL language

DQL data query language

SELECT syntax

Query all student information (all columns, low efficiency)

Insert picture description here

select * from student;

[External link image transfer failed. The source site may have an anti-leech link mechanism. It is recommended to save the image and upload it directly (img-vMoGHcIC-1597365958916)(C:\Users\ZMZDMX\AppData\Local\Temp\1597282545165.png)]

Query specified column (student number, name) is not case sensitive

SELECT studentno,studentname FROM student;

[External link image transfer failed. The source site may have an anti-leech link mechanism. It is recommended to save the image and upload it directly (img-WTIC91oA-1597365958917)(C:\Users\ZMZDMX\AppData\Local\Temp\1597282678305.png)]

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;

[External link image transfer failed. The source site may have an anti-leech link mechanism. It is recommended to save the image and upload it directly (img-3Ui0pUQT-1597365958919)(C:\Users\ZMZDMX\AppData\Local\Temp\1597283717143.png)]

  • 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;

[External link image transfer failed. The origin site may have an anti-leech link mechanism. It is recommended to save the image and upload it directly (img-fZsYRPFC-1597365958919)(assets/1597284776731.png)]

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

    [External link image transfer failed. The origin site may have an anti-leech link mechanism. It is recommended to save the image and upload it directly (img-rTlfxc1p-1597365958920)(assets/1597285518082.png)]

  • 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

    [External link image transfer failed. The source site may have an anti-leech link mechanism. It is recommended to save the image and upload it directly (img-zGav6GmO-1597365958921)(assets/1597286568438.png)]

Link query

  • Inner join. inner joinQuery 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 jointo 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 joinis opposite to above

      [External link image transfer failed. The origin site may have an anti-leech link mechanism. It is recommended to save the image and upload it directly (img-otBGBEMY-1597365958922)(assets/1597288792447.png)]

  • 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
Insert picture description here

case field when value then operation else operation end;

Insert picture description here

In many cases, the case field when value then operation when value then operation else operation end;

Full connection union: union and deduplication union all: union without deduplication

Guess you like

Origin blog.csdn.net/zmzdmx/article/details/107996647