当前位置:网站首页>**MySQL example 1 (query by multiple conditions according to different problems)**

**MySQL example 1 (query by multiple conditions according to different problems)**

2022-06-26 01:25:00 Mixed with bean curd and

MySQL Example 1

Multifaceted practice
1. Fallible knowledge

/* NULL And a null value
-NULL That is, store... In the field NULL value , A null value means that a field stores a null character (’’).

select length(NULL), length(''), length('1'),length('2');

+--------------+------------+-------------+-------------+
| length(NULL) | length('') | length('1') | length('2') |
+--------------+------------+-------------+-------------+
| NULL         |          0 |           1 |           1 |
+--------------+------------+-------------+-------------+
1 row in set

Look at the null value from above (’’) Is the length of the 0, It doesn't take up space ; and NULL The length is NULL, In fact, it takes up space .
NULL Columns need extra space in the row to record whether their values are NULL.

summary
1、 Null does not take up space ,NULL Value takes up space . When the field is not NULL when , You can also insert null values .

2、 When using IS NOT NULL perhaps IS NULL when , You can only find out that there is no such thing as NULL Of or for NULL Of , Can't find out the null value .

3、 Judge NULL use IS NULL perhaps is not null,SQL Statement function can use IFNULL() Function to handle , Use... To judge empty characters =’‘ perhaps <>’' To process .

4、 It's going on count() When counting the number of records in a column , If you use NULL value , The system will automatically ignore , But null values will be counted into it .

5、MySql If a column contains NULL, Then the index containing the column is invalid . This sentence is not very accurate .

6: In fact, it is the use of NULL Value or null (’’), Differentiate according to the actual business . I suggest that if there is no special business scenario in actual development , You can use null values directly .

Reference resources NULL And a null value https://blog.csdn.net/u010648555/article/details/94555199
MySQL (37k) keyword
https://blog.csdn.net/weixin_45851945/article/details/114287877

*/

/* Create foreign key format
CONSTRAINT fk_dept_personnel foreign key (d_id) references personnel(id)
CONSTRAINT Alias foreign key ( Foreign key table values ) references Main table ( Main table value )
*/
– COMMENT Set notes when creating a table
– auto_increment Set the gain ( integer )
– default Set the default value
– IF EXISTS Judge whether it exists ( If there is )

2. Basic create exercises database statement

– Create exercise database The character set is utf8

create DATABASE Exercises_One CHARSET=utf8;

– Switch to Exercises_One Next

use Exercises_One;

– Delete the table with the same name before creating the table ( Structure and data ) In general, duplicate name tables cannot appear in the same database

DROP TABLE IF EXISTS personnel;
create table personnel (
	id int(11) not null auto_increment COMMENT ' Primary key id',
	name varchar(255) DEFAULT NULL COMMENT ' name   The default is empty. ',
	age int(11) default null COMMENT ' Age   The default is empty. ',
	salary int(10) default null COMMENT ' Wages   The default is empty. ',
	leader int(11) default null COMMENT ' Leader   The default is empty. ',
	menpai varchar(255) default null COMMENT ' Sects   The default is empty. ',
primary key (id) COMMENT ' Primary key '
)charset=utf8;
select * from personnel;

– insert data

INSERT INTO personnel VALUES(DEFAULT,' Zhang Feng ',25,10000,0,' Wudang ');
INSERT INTO personnel VALUES(DEFAULT,' zhang wuji ',25,8000,0,' Huashan Mountain ');
INSERT INTO personnel VALUES(DEFAULT,' yue buqun ',25,6500,0,' Songshan Mountain ');
INSERT INTO personnel VALUES(DEFAULT,' invincible eastern ',25,12000,0,' The sun and the moon ');
INSERT INTO personnel VALUES(DEFAULT,' Linghu Cong ',25,4000,0,' Wudang ');
INSERT INTO personnel VALUES(DEFAULT,' Jingtian ',25,2000,0,' Huashan Mountain ');
INSERT INTO personnel VALUES(DEFAULT,' Nightshade ',25,10000,0,' Songshan Mountain ');
INSERT INTO personnel VALUES(DEFAULT,' Maomao ',25,10000,0,' The sun and the moon ');
INSERT INTO personnel VALUES(DEFAULT,' White tofu ',25,6500,0,' Changbai Mountain ');
INSERT INTO personnel VALUES(DEFAULT,'Farke',25,10000,0,' Wudang ');
INSERT INTO personnel VALUES(DEFAULT,'Alex',25,10000,0,'Java');

– update set according to id modify age

update personnel set age=27 WHERE id=2;
update personnel set age=28 WHERE id=3;
update personnel set age=23 WHERE id=4;
update personnel set age=29 WHERE id=5;
update personnel set age=30 WHERE id=6;
update personnel set age=28 WHERE id=7;
update personnel set age=28 WHERE id=8;
update personnel set age=26 WHERE id=9;
update personnel set age=23 WHERE id=10;

– Delete the table with the same name before creating the table ( Structure and data ) In general, duplicate name tables cannot appear in the same database

DROP TABLE IF EXISTS dept;
create table dept(
d_id int(11) not null auto_increment PRIMARY key COMMENT ' Lord 、 Foreign keys ',
d_name varchar(255) not null  COMMENT ' Department name ',
adress VARCHAR(255) not null COMMENT ' Address ',
CONSTRAINT fk_dept_personnel foreign  key (d_id) references personnel(id)
)CHARACTER set=utf8;
select * from dept ;
select * from personnel;

– insert data

INSERT INTO dept VALUES(DEFAULT,' Wudang ',' Henan ');
INSERT INTO dept VALUES(DEFAULT,' Huashan Mountain ',' hubei ');
INSERT INTO dept VALUES(DEFAULT,' Songshan Mountain ',' Shenzhen ');
INSERT INTO dept VALUES(DEFAULT,' The sun and the moon ',' Guangzhou ');
INSERT INTO dept VALUES(DEFAULT,' Changbai Mountain ',' urumqi ');
INSERT INTO dept VALUES(DEFAULT,' Mercenaries ',' xinjiang ');

– Delete the table with the same name before creating the table ( Structure and data ) In general, duplicate name tables cannot appear in the same database

DROP TABLE IF EXISTS leaders;
create table leaders(
 l_id int(10) primary key  not null auto_increment COMMENT' Primary key ',
 l_name varchar(255) default null COMMENT ' name   The default is empty. ',
 CONSTRAINT fk_dleaderst_personnel foreign  key (l_id) references personnel(leader)
)CHARSET=utf8;

3. Test example

– 1. Query all personnel information


select * from personnel;

– 2. Only the name and age of the person are queried


select name,age from personnel;

– 3. Query age is 28 Who are the aged people


select * from personnel where age=28;

– 4. Inquire about 60 Who are the people under the age of


select * from personnel where age<60;

– 5. Inquire about 27 Over years old with salary greater than 8000 Who are the staff of


select * from personnel where age>27 and salary>8000;

– 6. Check the last name [ Zhang ] Who are the staff of
– 'like and %' Use a combination of Fuzzy query


select * from personnel where name LIKE' Zhang %';

– Regular expressions REGEXP


select * from personnel where name REGEXP '^ Zhang ';

– 7. Query which personnel belong to Wudang / Huashan Mountain / Songshan Mountain


select * from personnel where menpai in(' Wudang ',' Huashan Mountain ',' Songshan Mountain ');

select * from personnel where menpai=‘ Huashan Mountain ’;
select * from personnel where menpai=‘ Wudang ’;
select * from personnel where menpai=‘ Songshan Mountain ’;

– 8. Check salary at 5000-8900 Who are the staff of
– Logical operators <= >=


select * from personnel where 5000<=salary and salary>=8900;

– 9. Query all personnel , It is required to arrange the salary in reverse order
– flashback order bay positive sequence desc


select * from personnel ORDER BY salary;

-- select * from personnel DESC salary;

– 10. Who is the leader of linghuchong
– You need an association table , There is no information here


select * from personnel where id = (select leader from personnel where name = ' Linghu Cong ');

– 11. Query the maximum salary in the personnel table
– MAX() Maximum


select MAX(salary) from personnel;

– 12. Query the minimum wage in the personnel table
– MIN() minimum value


select  MIN(salary) FROM personnel;

– 13. Query the average salary of all employees
– AVG() The average


select AVG(salary) from personnel;

– 14. Query the total salary of all employees
– SUM() total


select SUM(salary) from personnel;

– 15. Query the current number of employees
– COUNT() Count the total


select COUNT(*) from personnel;
select COUNT(name) from personnel;
select COUNT(id) from personnel;

– 16. Query which sects in the current Wulin
– DISTINCT Clear duplicates ( duplicate removal )


select DISTINCT menpai from personnel ;

– GROUP BY grouping


select DISTINCT menpai from personnel GROUP BY  menpai;

– 17. Inquire about Wudang sect Who is the highest salary


select  MAX(salary),NAME from personnel where menpai=' Wudang ';

– 18. Query the average salary of each sect
– GROUP BY Group query
– ORDER BY array


select  menpai,AVG(salary) from personnel GROUP  BY menpai;

– 19. Query which sects in the current Wulin have an average salary greater than 8000 And in reverse order of salary
– HAVING
– DESC Reverse order Combine ORDER BY Use
– HAVING Words allow us to filter all kinds of data into groups ,where Filter records before aggregating , That is to say, it works on group by and HAVING Before a sentence . and HAVING Clause filters group records after aggregation . My understanding is that there is no such data in the real table , These data are survived by some functions .


select menpai,AVG(salary)>8000 from personnel  GROUP BY menpai HAVING AVG(salary)>8000 ORDER BY AVG(salary) DESC;

– 20. Query the... In the current personnel table 3 Data to 7 Data
– LIMIT Paging query Start and end positions can be specified


select * from personnel LIMIT 2, 5;

– 21. Check which sects have no disciples

select * from personnel GROUP BY menpai HAVING COUNT(*) =1;

select *from personnel GROUP BY menpai HAVING COUNT(*)=1;

– 22. Ask which disciples of Wudang sect there are


select * from personnel where menpai=' Wudang ';

– 23. Inquire about Each sect Of The sum of wages In reverse order / Positive order
– In what order Just remember to use grouping and sorting together
– positive sequence ASC


select menpai,SUM(salary) from personnel GROUP BY menpai ORDER BY SUM(salary) ASC;

– flashback DESC

select menpai,SUM(salary) from personnel GROUP BY menpai ORDER BY SUM(salary) DESC;

– 24. Delete the personnel with duplicate salary , Please keep the oldest person
Generally, it is a logical deletion ,


DELETE FROM personnel where id in(
    SELECT id FROM (SELECT id from personnel where salary in(SELECT salary FROM personnel GROUP BY salary HAVING count(*)>1)
    AND age not in (SELECT MAX(age) FROM personnel GROUP BY salary HAVING count(*)>1)) as temp
);

– 25. Take Wudang sect Zhang Sanfeng It is amended as follows Zhang Feng

update  set name=' Zhang Sanfeng ' where name=' Zhang Feng ';

select * from personnel;
select * from dept;

– 26. Raise the salary of all sect eldest brothers 10%, But does not include Alex.
– != Not It doesn't contain


update personnel set salary = salary+salary*0.1 where leader = 0 and name !='Alex';

– 27. Check which people's sects have registered their geographical locations .
– Multi table and multi column query
– DISTINCT Repeat clearly ( duplicate removal )


select DISTINCT adress,d_name from personnel,dept where menpai=d_name;

– 28. Query the location information of all staff sects , If there is no location information, it will not be displayed
– LEFT JOIN Left connection on Followed by link judgment conditions


select name,adress from personnel LEFT JOIN dept on menpai=d_name ;

– 29. Who are the members of the sects in Hubei Province .


select name,adress from personnel LEFT JOIN dept on menpai=d_name where adress=' hubei ';

– 30. The salary of sects in Shaanxi Province is less than 5000, Older than 20 Who are the people aged , Sort by primary key in reverse order


SELECT * FROM personnel INNER JOIN dept on personnel.menpai = dept.d_name
AND dept.adress = ' hubei ' and personnel.salary <5000 AND personnel.age >20 ORDER BY personnel.id DESC;

原网站

版权声明
本文为[Mixed with bean curd and]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/177/202206252353285115.html