where condition query

1. Introduction to where conditional query

Use the where conditional query to filter the data in the table, and the records that meet the condition will appear in the result set.

Operators supported by the where statement:

  1. Comparison operator
  2. Logical Operators
  3. Fuzzy query
  4. Range query
  5. Null judgment

The syntax format of where conditional query is as follows:

select * from 表名 where 条件;
例:
select * from students where id = 1;

 

2. Comparison operator query

  1. Equal to: =
  2. Greater than:>
  3. Greater than or equal to: >=
  4. Less than: <
  5. Less than or equal to: <=
  6. Not equal to: != or <>

Example 1: Query students whose number is greater than 3:

select * from students where id > 3;

Example 2: Query students whose number is not greater than 4:

select * from students where id <= 4;

Example 3: Query students whose name is not "Huang Rong":

select * from students where name != '黄蓉';

Example 4: Query the students who have not been deleted:

select * from students where is_delete=0;

 

3. Logical operator query

  1. and
  2. or
  3. not

Example 1: Query female students whose number is greater than 3:

select * from students where id > 3 and gender=0;

Example 2: Query students whose number is less than 4 or have not been deleted:

select * from students where id < 4 or is_delete=0;

Example 3: Query students whose age is not between 10 and 15 years old:

select * from students where not (age >= 10 and age <= 15);

Description:

  • If you want to judge multiple conditions as a whole, you can combine'()'.

 

4. Fuzzy query

  1. like is a fuzzy query keyword
  2. % Means any number of any characters
  3. _ Represents an arbitrary character

Example 1: Query the student surnamed Huang:

select * from students where name like '黄%';

Example 2: Query students whose last name is Huang and whose "first name" is one word:

select * from students where name like '黄_';

Example 3: Query students whose surname is Huang or Jing:

select * from students where name like '黄%' or name like '%靖';

 

5. Range query

  1. between .. and .. means to query in a continuous range
  2. in means to query in a non-contiguous range

Example 1: Query students whose numbers are 3 to 8:

select * from students where id between 3 and 8;

Example 2: Query boys whose numbers are not 3 to 8:

select * from students where (not id between 3 and 8) and gender='男';

 

6. Null judgment query

  1. Determined to be empty use: is null
  2. Judge non-empty use: is not null

Example 1: Query students who did not fill in their height:

select * from students where height is null;

note:

  1. Cannot use where height = null to judge as empty
  2. Cannot use where height != null to judge non-empty
  3. null is not equal to'' empty string

 

7. Summary

  • Common comparison operators are >,<,>=,<=,!=
  • The logical operator and indicates that multiple conditions are true at the same time, or indicates that if one of multiple conditions is true, it is true, and not indicates that the condition is reversed.
  • The combination of like and% means any number of arbitrary characters, and the combination of like and _ means an arbitrary character
  • between-and limits the range of continuity in limits the range of discontinuity
  • Determined to be empty use: is null
  • Judge non-empty use: is not null

Guess you like

Origin blog.csdn.net/weixin_48135624/article/details/115232417