Illustration of MySQL database installation and actual operations

Source: Internet
Author: User
Tags check character

This article describes how to install and operate a MySQL database. Do you have a headache in obtaining an illustration of MySQL database installation and actual operations? If this is the case, the following articles will provide you with corresponding solutions.

1. Diagram of MySQL database installation and operation: initial database

(Best combination with PHP) database installation and operations

Width = "505" height = "420"/>

B. Direct representation: select name 'name' from students order by age

2. Exact search:

A. Use in to specify the range: select * from students where native in ('hunan ', 'sichuan ')

B. between... and: select * from students where age between 20 and 30

C. "=": select * from students where name = 'hill'

D. like: select * from students where name like 'Li % '(Note that there is "%" in the query condition, it indicates that it is partially matched and there is information in it successively, that is, search for matching items starting with "Li. Therefore, to query all objects with "Li", run the following command: '% Li %'. If the second word is Li, it should be '_ Li %', '_ li', or '_ Li _'.)

E. [] match check character: select * from courses where cno like '[AC] %' (The Relationship Between or and "in (...) "similar, and" [] "can represent a range, such as: select * from courses where cno like '[A-C] % ')

3. Processing Time type variables

A. smalldatetime: Process strings directly, for example:

Select * from students where birth> = '2017-1-1 1' and birth <= '2017-12-31'

4. Diagram of MySQL database installation and operations. Set Functions

A. count () summation, for example: select count (*) from students (total number of students)

B. avg (column) calculates the mean, for example, select avg (mark) from grades where cno = 'b2'

C. max (column) and min (column), max and min

5. group

Used for statistics, such as the total number of group queries:

 
 
  1. select gender,count(sno)   
  2. from students  
  3. group by gender 

(Check the number of male and female students)

Note: from which point of view the group is, from which column "group"

For multiple groups, you only need to list the grouping rules. For example, to query the number of male and female students in each major, the grouping rules are: grade, mno, and gender. Therefore, "group by grade, mno, gender"

 
 
  1. select grade, mno, gender, count(*)  
  2. from students  
  3. group by grade, mno, gender 

Normally, group is also used with having. For example, if a student who fails to take more than one course is queried, the student IDs are classified by student ID (sno) as follows:

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

6. UNION

Merge query results, such:

 
 
  1. SELECT * FROM students
  2. WHERE name like 'sheet %'
  3. UNION [ALL]
  4. SELECT * FROM students
  5. WHERE name like 'Li %'

7. Diagram of MySQL database installation and operations. Multi-Table query

A. Internal Connection

 
 
  1. select g.sno,s.name,c.coursename   
  2. from grades g JOIN students s ON g.sno=s.sno  
  3. JOIN courses c ON g.cno=c.cno 

(Note that aliases can be referenced)

B. External Connection

B1. left join

 
 
  1. select courses.cno,max(coursename),count(sno)   
  2. from courses LEFT JOIN grades ON courses.cno=grades.cno   
  3. group by courses.cno 

Left join feature: displays all items in all left tables, even if some of the items are not completely filled in.

The left Outer Join returns the rows that exist in the left table but do not exist in the right table, plus the rows of the inner join.

B2. right join

Similar to left join

B3. full connection

 
 
  1. select sno,name,major   
  2. from students FULL JOIN majors ON students.mno=majors.mno 

Show all content in both tables

C. Self-connection

 
 
  1. select c1.cno,c1.coursename,c1.pno,c2.coursename   
  2. from courses c1,courses c2 where c1.pno=c2.cno 

Use aliases to solve the problem.

D. Cross join

 
 
  1. select lastname+firstname from lastname CROSS JOIN firstanme 

Equivalent to playing the flute Product

8. nested Query

A. Use the keyword IN, for example, to query the hometown of Li Shan:

 
 
  1. Select * from students
  2. Where native in (select native from students where name = 'hill ')

B. Use the keyword EXIST. For example, the following two sentences are equivalent:

 
 
  1. select * from students  
  2. where sno in (select sno from grades where cno=’B2’)  
  3. select * from students where exists   
  4. (select * from grades where   
  5. grades.sno=students.sno AND cno=’B2’)  

9. sorting order

A. There are two methods for sorting order: asc Ascending order and desc descending order.

B. sorting order can be arranged according to a certain item in the query condition, and this can be represented by numbers, such:

 
 
  1. select sno,count(*) ,avg(mark) from grades   
  2. group by sno  
  3. having avg(mark)>85  
  4. order by 3  

10. Diagram of MySQL database installation and operations. Miscellaneous

A. names with spaces should be enclosed.

B. You can use null to determine specific queries without data in a column, such as select sno and courseno from grades where mark IS NULL.

C. Differentiate the differences between any and all used in nested queries. any is equivalent to the logical operation "|", while all is equivalent to the logical operation "&".

D. Be careful when querying negative meanings:

For example, students who have not taken the 'b2' course:

 
 
  1. select students.*  
  2. from students, grades  
  3. where students.sno=grades.sno  
  4. AND grades.cno <> ’B2’  

The preceding query method is incorrect. For the correct method, see the following:

 
 
  1. select * from students  
  2. where not exists (select * from grades   
  3. where grades.sno=students.sno AND cno='B2')  

11. Solution to multiple difficult nested queries:

For example, students who take all courses:

 
 
  1. select *  
  2. from students  
  3. where not exists ( select *  
  4. from courses  
  5. where NOT EXISTS   
  6. (select *  
  7. from grades  
  8. where sno=students.sno  
  9. AND cno=courses.cno)) 

Exclusive: select from the student table to exclude those with no courses. Use not exist. Because the subject is a course, you can find the second query in the course table and exclude those that have selected the course.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.