MySQL 查詢語句篇


目錄

  • select查詢全表數據
  • where的簡單應用
  • in和not in的用法
  • between...and的用法
  • like和%的語法
  • case when的用法

 

1.1、select查詢全表數據

select * from student;

//這里我創建了一個表插入了一點數據,通過select語法可以查看所有的數據

 

2.1、where的簡單應用

create table student
(
    sno int auto_increment primary key,
    sname char(10) not null,
    score varchar(10)
);

insert into student(sname,score)
value("張三","22"),
("李四","33"),
("王五","44"),
("老六","99.2"),
("小七","87.3"),
("帥十","77.3");

select * from student where id  < 5;

//這里查詢了id小於5的學生信息

select * from student where id = 5;

//這里查詢了id為5的學生信息

select * from student where sname = "帥十";

//這里查詢了名字為帥十的學生信息

select sname , score from student;

//這里只顯示名字和成績的列

 

3.1、in和not in的簡單用法

create table student
(
    sno int auto_increment primary key,
    sname char(10) not null,
    score varchar(10)
);

insert into student(sname,score)
value("張三","22"),
("李四","33"),
("王五","44"),
("老六","99.2"),
("小七","87.3"),
("帥十","77.3");


select * from student where sname in("李四","王五");

//這里我么篩選出名字為"李四","王五"的信息

select * from student where sname not in("李四","王五");

//not in則篩選出除了這兩個人的其他人的個人信息

select * from student where sno in(select sno from student where id < 5);

//這里同樣in里面可以在接select查詢語句,但是必須查出來的數據必須是外面sno能夠匹配的,如果子查詢語句查詢出來的是全部信息那么就無法匹配,必然錯誤。這里我們在in里面插入的子查詢語句選出了id小於5的sno對外sno進行匹配,實際工作不可能這么寫,這里我只是舉例。

 

4.1、between...and的用法

create table student
(
    sno int auto_increment primary key,
    sname char(10) not null,
    score int
);

insert into student(sname,score)
value("張三","22"),
("李四","33"),
("王五","44"),
("老六","99"),
("小七","87"),
("帥十","77");

select * from student where score between 20 and 50;

//查詢成績在20到50之間的成績的學生信息

 

5.1、like和%的用法

create table student
(
     id int primary key,
     name char(10) nut null,
     sex char(5) default "",
    city char(10) not null,
    papers char(10)
);

insert  into student(id,name,city,papers)
value(1001,"王寧","廣州","station"),
(1002,"李思","成都","admint"),
(1003,"宋璐","上海","buinne"),
(1004,"郭科","杭州","counnt");

select * from student where name like'王%'//查詢名字開頭帶王的學生信息

select * from student where name like'%王%';

//查詢名字中間帶王的學生信息

select * from student where name like'%王';

//查詢名字結尾帶王的學生信息

select * from student where papers like binary'%in%';

//查詢證件papers中帶有in學生信息,這里加入binary說明區分大小寫

select * from student where city not like'%海';

//這里查詢城市中不以海結尾的學生的基本信息

 

6.1、case...when的用法

create table student
(
     id int primary key,
     name char(10) nut null,
     sex char(5) default "",
    city char(10) not null,
    papers char(10)
);

insert  into student(id,name,city,papers)
value(1001,"王寧","廣州","station"),
(1002,"李思","成都","admint"),
(1003,"宋璐","上海","buinne"),
(1004,"郭科","杭州","counnt");

select sno,case city 
when "廣州" then "廣東"
when "成都" then "四川"
when "上海" then "上海"
when "杭州" then "浙江"
else "其他" end as "city"
from student;

//這里的case when把所有城市重命名為省會,進行顯示。一個一個進行匹配,匹配不上的自動被列到其他這個項里。


免責聲明!

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



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