Relational database (6): MySQL query data and where and like clauses (regular)

Query data

grammar

# 以下为在MySQL数据库中查询数据通用的 SELECT 语法:

SELECT column_name,column_name
FROM table_name
[WHERE Clause]
[LIMIT N][ OFFSET M]
  • One or more tables can be used in a query statement, separated by commas (,), and a WHERE statement can be used to set query conditions.
  • The SELECT command can read one or more records.
  • You can use an asterisk (*) to replace other fields, the SELECT statement will return all field data of the table
  • You can use the WHERE statement to include any condition.
  • You can use the LIMIT property to set the number of records returned.
  • You can specify the data offset at which the SELECT statement starts the query with OFFSET. By default the offset is 0.

Get data via command prompt

example

# 以下实例将返回数据表 hanscal_tb 的所有记录:
select * from hanscal_tb;

WHERE child clause

grammar

# 以下是 SQL SELECT 语句使用 WHERE 子句从数据表中读取数据的通用语法:

SELECT field1, field2,...fieldN FROM table_name1, table_name2...
[WHERE condition1 [AND [OR]] condition2.....
  • In the query statement, you can use one or more tables, separate the tables with commas, and use the WHERE statement to set query conditions.
  • You can specify any condition in the WHERE clause.
  • You can specify one or more conditions using AND or OR.
  • The WHERE clause can also be used with SQL's DELETE or UPDATE commands.
  • The WHERE clause is similar to the if condition in procedural languages, and reads the specified data according to the field value in the MySQL table.

The following is a list of operators that can be used in the WHERE clause.

Example: Suppose A is 10 and B is 20

operator describe example
= Equals sign, checks whether two values ​​are equal, and returns true if they are equal (A = B) returns false
<>, != Not equal, check if two values ​​are equal, return true if they are not equal (A != B) returns true
> Greater than sign, check whether the value on the left is greater than the value on the right, if the value on the left is greater than the value on the right, return true (A > B) returns false
< Less than sign, check whether the value on the left is less than the value on the right, if the value on the left is less than the value on the right, return true (A < B) returns true
>= Greater than or equal sign, check whether the value on the left is greater than or equal to the value on the right, if the value on the left is greater than or equal to the value on the right, return true (A >= B) returns false.
<= Less than or equal sign, check whether the value on the left is less than or equal to the value on the right, if the value on the left is less than or equal to the value on the right, return true (A <= B) returns true.

The WHERE clause is very useful if you want to read specified data in a MySQL data table.

Using the primary key as a conditional query in the WHERE clause is very fast.

If the given criteria do not have any matching records in the table, the query returns no data.


read data from command prompt

example

# 以下实例将读取 hanscal_tb 表中 hanscal_author 字段值为 hanke 的所有记录,该查询不区分大小写
SELECT * from hanscal_tb WHERE hanscal_author='hanke';

# 实例中使用了 BINARY 关键字,是区分大小写的,没有查询到值
SELECT * from hanscal_tb WHERE BINARY hanscal_author='Hanke'; 
Empty set (0.01 sec) 

String comparisons in MySQL's WHERE clause are case-insensitive. You can use the BINARY keyword to specify that string comparisons in the WHERE clause are case-sensitive. 

LIKE clause

Sometimes we need to get all the records that contain certain characters in the field, then we need to use the SQL LIKE clause in the WHERE clause.  The percent sign % character is used in the SQL LIKE clause to represent any character, similar to the asterisk * in UNIX or regular expressions. If the percent sign % is not used, the LIKE clause has the same effect as the equal sign =.

grammar

# 以下是 SQL SELECT 语句使用 LIKE 子句从数据表中读取数据的通用语法:

SELECT field1, field2,...fieldN 
FROM table_name
WHERE field1 LIKE condition1 [AND [OR]] filed2 = 'somevalue'
  • You can specify any condition in the WHERE clause.
  • You can use the LIKE clause in the WHERE clause.
  • You can use the LIKE clause in place of the equal sign =.
  • LIKE is often used with %, similar to a metacharacter search.
  • You can use AND or OR to specify one or more conditions.
  • You can use the WHERE...LIKE clause in a DELETE or UPDATE command to specify conditions.

Using the LIKE clause in the command prompt

example

# 以下示例将 hanscal_tb 表中获取 hanscal_author 字段中以 cal 为结尾的的所有记录:

mysql> use RUNOOB; 
mysql> SELECT * from hanscal_tb WHERE hanscal_author LIKE '%cal';

like match/fuzzy match, will be used in combination with % and _.

'%a' //Data ending with a 
'a%' //Data starting with a 
'%a%' //Data containing a 
'_a_' //'_a' with three digits and the middle letter is a 
/ /'a_' with two digits and ending with a 
//with two digits and starting with a

In the conditional query of where like, SQL provides four matching methods.

%: Indicates any zero or more characters. It can match characters of any type and length. In some cases, if it is Chinese, please use two percent signs (%%).

_: Represents any single character. Matches a single arbitrary character, it is often used to limit the character length of expressions in statements.

[]: Represents one of the characters listed in parentheses (like a regular expression). Specify a character, string, or range to match against any of them.

[^] : Represents a single character not listed in parentheses. Its value is the same as [], but it requires the matched object to be any character other than the specified character.

When the query content contains wildcards, the query for special characters "%", "_", "[" cannot be implemented normally due to the wildcards, but the query can be performed normally by enclosing the special characters with "[ ]" .

regular expression REGEXP

 Fuzzy matching is possible with  LIKE ...% . MySQL also supports the matching of other regular expressions. The REGEXP operator is used in MySQL for regular expression matching.

model describe
^ Matches the starting position of the input string. If the Multiline property of the RegExp object is set, ^ also matches the position after '\n' or '\r'.
$ Matches the end of the input string. If the Multiline property of the RegExp object is set, $ also matches the position before '\n' or '\r'.
. Matches any single character except "\n". To match any character including '\n', use a pattern like '[.\n]'.
[...] character collection. Matches any one of the included characters. For example, '[abc]' can match 'a' in "plain".
[^...] A collection of negative characters. Matches any character not included. For example, '[^abc]' matches the 'p' in "plain".
p1|p2|p3 Match p1 or p2 or p3. For example, 'z|food' matches "z" or "food". '(z|f)ood' matches "zood" or "food".
* Matches the preceding subexpression zero or more times. For example, zo* matches "z" as well as "zoo". * is equivalent to {0,}.
+ Matches the preceding subexpression one or more times. For example, 'zo+' matches "zo" and "zoo", but not "z". + is equivalent to {1,}.
{n} n is a non-negative integer. Matches a certain number of n times. For example, 'o{2}' would not match the 'o' in "Bob", but would match the two o's in "food".
{n,m} Both m and n are non-negative integers, where n <= m. Matches at least n times and at most m times.

example

# 查找name字段中以'st'为开头的所有数据:
mysql> SELECT name FROM hanscal_tb WHERE name REGEXP '^st';

# 查找name字段中以'ok'为结尾的所有数据:
mysql> SELECT name FROM hancal_tb WHERE name REGEXP 'ok$';

# 查找name字段中包含'mar'字符串的所有数据:
mysql> SELECT name FROM hanscal_tb WHERE name REGEXP 'mar';

# 查找name字段中以元音字符开头或以'ok'字符串结尾的所有数据:
mysql> SELECT name FROM hanscal_tb WHERE name REGEXP '^[aeiou]|ok$';

Guess you like

Origin blog.csdn.net/weixin_43145427/article/details/124142051