Query of conditional statements

Conditional statements

  • Where clause to filter the data table, the data symbols condition appears in the result set
  • The syntax is as follows:
select 字段1,字段2... from 表名 where 条件;
例:
select * from students where id=1;
  • Where the back support multiple operators, conditional processing
    • Comparison operation
    • logic operation
    • Fuzzy query
    • Range queries
    • Empty judgment

Comparison Operators

  • Equal to: =
  • Greater than:>
  • Greater than or equal:> =
  • Less than: <
  • Less than or equal: <=
  • Does not mean:! = Or <>

Example 1: Query Missy age

select age from students where name='小乔'

Example 2: Query students below 20 years of age

select * from students where age<20

Example 3: The query is not in Beijing students home

select * from students where hometown!='北京'

Exercise:

1、查询学号是'007'的学生的身份证号
2、查询'1班'以外的学生信息
3、查询年龄大于20的学生的姓名和性别

Logical Operators

  • and
  • or
  • not

Example 1: Query female students younger than 20

select * from students where age<20 and sex='女'

Example 2: Query or female students 'class 1' students

select * from students where sex='女' or class='1班'

Example 3: non-student inquiry in Tianjin

select * from students where not hometown='天津'

Exercise:

1、查询河南或河北的学生
2、查询'1班'的'上海'的学生
3、查询非20岁的学生

Fuzzy query

  • like
  • % Represents any number of any character
  • _ Represents an arbitrary character

Example 1: Query student surnamed Sun

select * from students where name like '孙%'

Example 2: Query surnamed Sun and the name is a word of student

select * from students where name like '孙_'

Example 3: Query student named Joe

select * from students where name like '%乔'

Example 4: Query name containing white students

select * from students where name like '%白%'

Exercise:

1、查询姓名为两个字的学生
2、查询姓百且年龄大于20的学生
3、查询学号以1结尾的学生

Range queries

  • It represents a non-contiguous in the range of

Example 1: Query hometown students in Beijing or Shanghai or Guangdong

select * from students where hometown in('北京','上海','广东')
  • between ... and ... represents a continuous range

Example 2: Query for students ages 18-20 in

select * from students where age between 18 and 20

Exercise:

1、查询年龄在18或19或22的女生
2、查询年龄在20到25以外的学生

Empty judgment

  • Note: null and '' are different
  • Empty sentence is null

Example 1: The query does not fill out the identity of the student

select * from students where card is null
  • Sentenced to a non-null is not null

Example 2: Query filled out student ID card

select * from students where card is not null
Published 240 original articles · won praise 77 · views 80000 +

Guess you like

Origin blog.csdn.net/dpl12/article/details/104196898