MySQL數據庫where查詢條件有哪些?


使用where子句對表中的數據篩選,結果為true的行會出現在結果集中,語法如下:

select * from 表名 where 條件;

where后面的篩選條件有以下5種:

1.比較運算符

  • 等於:=

  • 大於:>

  • 大於等於:>=

  • 小於:<

  • 小於等於:<=

  • 不等於:!=

通過以下幾個例子來加深理解:

 

  • 查詢沒被刪除的學生
    select * from students where isdelete=0;

 

  • 查詢編號大於3的學生
    select * from students where id>3;
  • 查詢編號不大於4的科目
    select * from subjects where id<=4;
  • 查詢姓名不是“黃蓉”的學生
    select * from students where sname!='黃蓉';

     

2.邏輯運算符

  • and

  • or

  • not

例如:

  • 查詢編號大於3的女同學
    select * from students where id>3 and gender=0;
  • 查詢編號小於4或沒被刪除的學生
    select * from students where id<4 or isdelete=0;

     

3.模糊查詢

  • like

  • %表示任意多個任意字符

  • _表示一個任意字符

例:

  • 查詢姓黃的學生

    select * from students where sname like '黃%';
  • 查詢姓黃並且名字是一個字的學生
    select * from students where sname like '黃_';
  • 查詢姓黃或叫靖的學生
    select * from students where sname like '黃%' or sname like '%靖%';

 

4.范圍查詢

  • in表示在一個非連續的范圍內

  • between ... and ...表示在一個連續的范圍內

例:

  • 查詢編號是1,3,8的學生

    select * from students where id in(1,3,8);
  • 查詢編號是3-8的學生
    select * from students where id between 3 and 8;

 

5.空判斷

  • 注意:null與空字符串''是不同的

  • 判空is null

  • 判非空is not null

例:

  • 查詢沒有填寫地址的學生
    select * from students where hometown is null;
  • 查詢填寫了地址的女生
    select * from students where hometown is not null and gender=0;
 
        

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM