MySQL basics --- data query

One. Sort query results

Order by (default sorting method 'ascending asc', descending desc)

1) Single column sorting

select * from table name order by field name sorting method (asc / desc);

2) Multi-column sorting

select * from table name order by field name 1 sorting method (asc / desc), field name 2 sorting method (asc / desc);

length (field name) --- can calculate the length of the string

Principle: still from left to right, if the content of field 1 is the same, then arrange according to the content and method of field name 2

two. Use Limit to limit the number of query results

Select * from table name limit initial position index, quantity;

Note:
1) The index of the first data position is 0

2) The subscript of the initial position can be omitted and is 0

  1. Limit syntax requirements must be written after order by (when not written by default, they are sorted in order of insertion)

Note: SELECT * from (select * from guestinfo LIMIT 5) t1 ORDER BY t1.guestIncome asc

In addition, the parentheses are arranged in the order of insertion to take the first five, so that a new table is formed, the table name is t1. Then on this new table

put in order

three. Fuzzy query

select * from table name where field name like 'ambiguous content and wildcards'

Note: Only data of string type can use like and wildcard wildcards:

  1. %: Match 0 to more arbitrary characters (you can write before, after, and in)

For example: select * from table name where name like 'Zhang%' (matches all data that starts with Zhang in the name field)

  1. : Match an arbitrary character, two if you want two _

For example: select * from table name where name like 'Zhang_' (match the two words of data at the beginning of the name field Zhang)

  1. []: Used to specify a character set (note that the previous keywords are: rlike or regexp, the date class is not applicable)

For example: rLike '5 [a, b, c]' matches: 5a, 5b, 5c

rLike '5 [abc]' can match: 5abc

Note: indicates that one of them can be selected

rLike '5 [a%]' can match: 5a%

rLike '5 [a_]' can match: 5a_

Note: The% or _ in square brackets are only characters, not wildcards

rLike '[ad]' can match: a, b, c, d

rLike '[^ a]' can match: all that are not a

four. Conditional operator

  1. in == or

Select * from table name where field name in ('content 1', 'content 2')

  1. between contains small values ​​and contains large values ​​== and

Can be numeric and date type, the value includes the boundary value.

Select * from table name where field name between '1970-01-01' and '1980-10-10';

Note: The advantages of this type of conditional operator and the previously learned or / and are: when there are many selection conditions, the use of this type of operation is more efficient

Published 32 original articles · Likes 96 · Visits 1583

Guess you like

Origin blog.csdn.net/qq_44534541/article/details/105500895