Improvement and optimization of SQL statement capabilities

Pay attention to the official account "Liubai Lingyi", and you can consult at any time if you have any technical questions that you don't understand!


Crazy Code http://www.crazycoder.cn/ ĵ:http:/ http://www.crazycoder.cn/DataBase/Article69022.html

1. Display the column names in the query results:   

a. Use the as keyword: select name as 'name' from students order by age b. Directly express: select name 'name' from students order by age   

2. Accurate search:   

a. Use in to limit the scope: select * from students where native in ('Hunan', '4chuan') b. between...and: select * from students where age between 20 and 30 c. "=": select * from students where name = 'Li Shan' d, like:select * from students where name like 'Li%' (note that if there is a "%" in the query condition, the introduction indicates that it is a partial match and there is also sequence information in it, that is, search for " "李" at the beginning of the match, so if the query has "李" all objects should command: '%李%'; if the second word is Li, it should be '_李%' or '_李' or '_李_') e. Matching checker: select * from courses where cno like '[AC]%' (indicating or relationship is similar to "in(...)" and "" can represent range such as: select * from courses where cno like '[ AC]%')   

3. Processing of time type variables   

a, smalldatetime: directly process according to the string processing method, for example: select * from students where birth > = '1980-1-1' and birth <= '1980-12-31'   

4. set   

a, count summation such as: select count(*) from students (total number of students) b, avg (column) average such as: select avg(mark) from grades where cno='B2' c, max (column) and min (column) to find the maximum and minimum   

5. Grouping group   

Commonly used in statistics, such as checking the total number by group: select gender, count(sno) from students group by gender (check the number of male and female students)   

Note: From which angle to group from which column "group by"   

For multiple grouping, you only need to list the grouping rules. For example, if you query the number of male and female students in each session and major, then the grouping rules are:

Grade (grade), major (mno) and gender (gender) so there is "group by grade, mno, gender" select grade, mno, gender, count(*) from students group by grade, mno, gender   

Usually group is also used in conjunction with having. For example, to query students who failed more than one course, they are classified by student number (sno):

select sno,count(*) from grades where mark<60 group by sno having count(*)>1   

6. UNION   

Combine query results such as: SELECT * FROM students WHERE name like 'Zhang%'   

UNION [ALL] SELECT * FROM students WHERE name like ‘李%'   

7. Multi-table query   

a、内连接 select g.sno,s.name,c.coursename from grades g JOIN students s _disibledevent=>  

JOIN courses c _disibledevent=> (note that aliases can be quoted)   

b. Outer join

b1、左连接 select courses.cno,max(coursename),count(sno) from courses LEFT JOIN grades _disibledevent=> group by courses.cno   

Left join features: display all items in the left table even if some of the items in the data are not completely filled, the left outer join returns those that exist in the left table but there are no rows in the right table plus inner join rows   

b2, right join and left join are similar to b3, full join select sno, name, major from students FULL JOIN majors _disibledevent=>display all the contents in the tables on both sides   

c. Self connection select c1.cno, c1.coursename, c1.pno, c2.coursename from courses

c1.courses c2.where c1.pno=c2.cno uses aliases to solve the problem   

d. Cross connection select lastname+firstname from lastname CROSS JOIN firstname is equivalent to doing Cartesian product

8. Nested queries   

a. Use the keyword IN, such as querying Lishan fellow countrymen: select * from students where native in (select native from students where name='Li Shan') b. Use the keyword EXIST, for example, the following two sentences are equivalent: select * from students where sno in (select sno from grades where cno='B2') select * from students where exists (select * from grades where grades.sno=students.sno AND cno='B2')   

9. About sorting order

a. There are two ways of thinking about sorting order: asc ascending order and desc descending order b. For sorting order, it can be arranged according to a certain item in the query condition and this item can be represented by numbers such as: select sno, count(*), avg(mark) from grades group by sno having avg(mark)>85 order by 3   

10. Others

a. For the identification name with spaces, "" should be enclosed. b. For specific queries without data in a certain column, you can use null to judge, such as select sno, coursesno from grades where mark IS NULL c. Pay attention to distinguish between using any and in nested queries The difference between all is that any is equivalent to the logical operation "||" and all is equivalent to the logical operation "&&" d. Be careful to enter the trap when doing negative meaning queries:   

For students who have not taken the 'B2' course: select students.* from students, grades where students.sno=grades.sno AND grades.cno <> 'B2'   

The above query method is correct, see below: select * from students where not exists (select * from grades where grades.sno=students.sno AND cno='B2')   

11. Ideas for solving difficult multiple nested queries:   

For students who have taken all courses: select * from students where not exists ( select * from courses where NOT EXISTS

(select * from grades where sno=students.sno AND cno=courses.cno))

Guess you like

Origin blog.csdn.net/lghtdw1314/article/details/123636757