1. 程式人生 > >MySQL之where查詢

MySQL之where查詢

比較運算

只顯示id,name:     select id,name from students;
消除重複行distinct
select distinct gender from students;
條件
select * from students where id>3;
select * from students where isDelete=0;

邏輯運算

  • and
  • or
  • not
select * from students where id>3 and gender=0;
select * from students where id<4 or isDelete=0
;

模糊查詢

  • like
  • % : 表示任意多個字元
  • _ : 表示一個任意字元

查詢姓羅的人:

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

查詢姓楊 且 名字是一個字的人:

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

查詢姓薛 且 名字兩個字的人:

select * from students where name like '薛__';

查詢姓羅 或者 叫嫣 的人:

select * from students where name like '羅%' or name like '%嫣';

範圍查詢

  • in :表示在一個非連續的範圍內
  • 查詢編號是1或3或8的人
select * from students where id in(1,3,8);
  • between ... and ... 表示在一個連續的範圍內(between後小數,and後大數)
  • 查詢id是3到8的人
select * from students where id between 3 and 8;

select * from students whereid between 3 and 8andgender=1;

空判斷

  • null與''不相同
  • 判斷: is null
select * from students where birthday is
null; select * from students where birthday is not null;

優先順序

  • 小括號, not, 比較運算子, 邏輯運算子
  • and比or先運算,如果同時出現並希望先算or,需要結合()使用