Several common keywords in sql query

Several common keywords in sql query

The group by grouping query
statement groups the result set according to one or more columns. We can use COUNT, SUM, AVG, and other functions on the grouping column.

SELECT apply_number,COUNT(brand),brand FROM vehicle_purchase_invoice GROUP BY brand;

Having keywords
allows us to filter all kinds of data after grouping, where clauses filter records before aggregation, that is to say, it acts before group by and having words. And the having word filters the group records after aggregation

SELECT apply_number,COUNT(brand),brand FROM vehicle_purchase_invoice GROUP BY brand HAVING COUNT(brand)>1;

Limit keyword The
first number represents the starting number (for example: 3, which means starting from 3, but does not contain 3), the second number represents the display number

SELECT * FROM vehicle_purchase_invoice LIMIT 3,3;

Order by
is sorted by certain fields. Order by is followed by a sort field. If there is where in the statement, order by must be placed after where. The default sorting of order by is ascending (ASC), and descending (DESC) is not written. Ascending by default.

SELECT apply_number,brand FROM vehicle_purchase_invoice ORDER BY apply_number DESC;

in: Allow multiple values ​​to be specified in the where clause 5, (1,2,3)
between…and…: Take out a range of values ​​between 4 and 6
Not in: Not in (1,2,3)

Like
LIKE is usually used together with the wildcard %.% means the content that appears in the wildcard pattern. The LIKE syntax without wildcard% means an exact match. The actual effect is the same as the = equal operator, and% after it means it starts with the current character , And the following can be any character,% put in front means the end of the current character, put on both sides means it contains the current character.

After the UNION
data is queried according to certain query conditions, the results are combined and displayed. At this time, the
union and union all keywords are needed to realize this function. The main difference between union and union all is that union all is the result The sets are directly merged together, and union is the result of mirroring the result of union all once distinct and removing duplicate records.

SELECT layer FROM vehicle_purchase_invoice WHERE brand="奥迪" UNION ALL SELECT layer FROM sys_location WHERE type='市';

Guess you like

Origin blog.csdn.net/Krystal_RonghuiLi/article/details/107999903