The first table below
select search query
- Retrieve a single column
SELECT name FROM `user`
- Retrieve multiple columns (be sure to add a comma between the column names, do not add the last column name)
SELECT name,age FROM `user`
- Retrieve all columns (wildcard * is used, although it saves trouble but retrieves unnecessary columns, it usually reduces the performance of retrieval and application)
SELECT * FROM `user`
- Retrieve different rows (the DISTINCT keyword is before the column)
SELECT DISTINCT age FROM `user`
limit Limit query results
Use the limit keyword to limit the query results
- Specify the start line and the number of lines to retrieve. If no start line is specified, the default is 0 lines. If there are not enough lines, it returns as many as it can return.
SELECT * FROM `user` LIMIT 2
SELECT * FROM `user` LIMIT 2,1
ORDER BY sort (its position is after FROM, before LIMIT)
The results of SQL statement retrieval generally have no order, if you want to sort it, you need to use the ORDER BY keyword
- Sort by single column
SELECT * FROM `user` ORDER BY age
- Sort by multiple columns (Retrieve the user table, sort the results by two columns, first sort by age, and sort by name for rows with the same age value, if all age values are the same, they will not be sorted by name)
SELECT * FROM `user` ORDER BY age,name
- Specify the sorting direction ( data sorting defaults to ascending order (from A to Z), you can also use descending order, but you must specify the keyword DESC)
SELECT * FROM `user` ORDER BY age DESC
- Specify the sort direction and sort by multiple columns (first descending by age, then sort by name on the same row of age)
SELECT * FROM `user` ORDER BY age DESC,name
- Find the highest or lowest value in a column
SELECT * FROM `user` ORDER BY age LIMIT 1
SELECT * FROM `user` ORDER BY age DESC LIMIT 1