This article uses examples to describe the commonly used SQL statements in the MySQL database. Share with you for your reference, the details are as follows: The basic MySQL SQL statements were described earlier. Here we continue to summarize the advanced content of SQL statements. Advanced SQL Statements 1. Query fields: ————Query all fields select * from table name; ————Query the specified field select field name, field name... from table name; ————Multiple data table connection query select table name.field name, table name.field name… from table name; ————Use as to alias the table select table alias.field name from table name as table alias; ————Eliminate duplicate rows (distinct) select distinct field name from table name; 2. Conditional query: ————Comparison operators (>, <, =, !=) select * from table name where age >18; (<> also means !=) ————Logical operators (and, or, not) select * from table name where age>18 and age<28; (18 3. Sorting: ————Ascending select * from table name order by asc; (the default is asc, which can be omitted) ————Descending select * from table name order by desc; 4. Aggregate functions: ————Total count select count(*) from table name; ————Maximum value max select max(age) from table name; ————Minimum value min select min(age) from table name; ————sum select sum(age) from table name; ————Find the average value avg select avg(age) from table name; ————Round to the nearest decimal place select round(avg(age),2) from table name; (query the average age, round to two decimal places) 5. Grouping (key point): ————Group by select gender count(*) from table name group by gender; (group by gender, query gender and number of people) ————Group query (aggregate function, group_concat(), having) select gender avg(age) from table name group by gender; (query the average age of each gender) select gender group_concat(name) from table name group by gender; (group_concat(name) to view the group name) select gender count() from table name group by gender having count()>2 (having is similar to where, filtering condition, having can only be used for group by, where is used for table data) ————Summary with rollup select gender count(*) from table name group by gender with rollup; (add a new row at the end to show the summary result) 6. Pagination: ————Query the first n data (limit is usually written in the best, indicating that the data after the operation is displayed) select * from table name limit n; ————Pagination display select * from table name limit 0,3; (display 3 per page, the first page) select * from table name limit 3,3; (display 3 per page, the second page) select * from table name limit 6,3; (display 3 per page, the 3rd page) 7. Connection query (key point): ————inner join…on (inner join) select * from table name 1 inner join table name 2 on table name 1.cls_id=table name 2.id; (join table 1 cls.id and table 2 with the same id) select table name 1. field name 1, table name 2. field name 2 from table name 1 inner join indicates 2 on condition; ————left/right join…on (left/right/outer join) select * from tablename1 left/right join tablename2 on tablename1.cls_id=tablename2.id; (the query result is the data matched by the two tables and the data unique to the left table. For data that does not exist in the left/right table, use null to fill it) 8. Subquery: ————Scalar subquery (the result returned by the subquery is one data (one row and one column)) select * from table name where age > (select avg(age) from table name); ————Column subquery (the result returned is one column (one column with multiple rows)) select name from tablename1 where id in (select cls_id from tablename2); ————Row subquery (the result returned is a row (one row with multiple columns)) select * from table name where (height,age) = (select max(height),max(age) from table name); Readers who are interested in more MySQL-related content can check out the following topics on this site: "MySQL query skills", "MySQL common functions summary", "MySQL log operation skills", "MySQL transaction operation skills summary", "MySQL stored procedure skills" and "MySQL database lock related skills summary" I hope this article will be helpful to everyone's MySQL database design. You may also be interested in:
|
<<: How to run Python script on Docker
>>: JavaScript array merging case study
There are two types: (different browsers) 1. Avail...
Table of contents 1. CentOS7+MySQL8.0, yum source...
This article shares the specific code for drawing...
In the forum, I saw netizen jeanjean20 mentioned h...
First, download the installation package from the...
Error: Connection to blog0@localhost failed. [080...
Table of contents background How does element-ui&...
one. Mysql Binlog format introduction Mysql binlo...
Table of contents Two ways to solve the problem o...
Maybe I started to think wrongly, and my descript...
Table of contents Case scenario Solving the probl...
In order to download this database, it takes a lo...
This article example shares the specific code for...
Result:Implementation Code html <link href=...
Cancel the icon 1 in front of ul li Clear Value 1 ...