使用select 查詢數據

數據查詢語言DQL

#使用select 查詢數據,語法:

select   要查詢的屬性1,屬性2,屬性3
from     表名
where    條件
group by   分組
HAVING      分組以後的條件篩選
order by    排序
limit       分頁,限制

#舉例
如圖所示:已知有表person,進行相關的查詢。

在這裏插入圖片描述
在這裏插入圖片描述
|---------------------------------------------------------------------------------------------------------------------

where字段方法

在這裏插入圖片描述
使用and 和or 進行多條件過濾:
在這裏插入圖片描述
以下提供測試數據

create table employee(
	id int not null auto_increment primary key,
	name varchar(30) comment '姓名',
	sex varchar(1) comment '性別',
	salary int comment '薪資(元)'
);

insert into employee(name, sex, salary) values('張三', '男', 5500);
insert into employee(name, sex, salary) values('李潔', '女', 4500);
insert into employee(name, sex, salary) values('李小梅', '女', 4200);
insert into employee(name, sex, salary) values('歐陽輝', '男', 7500);
insert into employee(name, sex, salary) values('李芳', '女', 8500);
insert into employee(name, sex, salary) values('張江', '男', 6800);
insert into employee(name, sex, salary) values('李四', '男', 12000);
insert into employee(name, sex, salary) values('王五', '男', 3500);
insert into employee(name, sex, salary) values('馬小龍', '男', 6000);
insert into employee(name, sex, salary) values('龍五', '男', 8000);
insert into employee(name, sex, salary) values('馮小芳', '女', 10000);
insert into employee(name, sex, salary) values('馬小花', '女', 4000);

執行完畢,查詢儲存情況,存儲正常。
在這裏插入圖片描述
|---------------------------------------------------------------------------------------------------------------------

題1:篩選所有男性員工的工資

select name,salary from employee where sex= '男';

結果:
在這裏插入圖片描述
|---------------------------------------------------------------------------------------------------------------------

題2:篩選所有女性員工的完整信息

select * from employee where sex= '女';

結果:
在這裏插入圖片描述
|---------------------------------------------------------------------------------------------------------------------

題3:篩選所有薪資大於10000的員工的完整信息

select * from employee where salary >=10000;

結果:
在這裏插入圖片描述

|---------------------------------------------------------------------------------------------------------------------

題4:篩選所有薪資大於10000,小於12000的員工的完整信息

select * from employee where salary between 10000 and 12000;

結果:
在這裏插入圖片描述
|---------------------------------------------------------------------------------------------------------------------

題5:篩選所有性別是男,薪資大於10000的員工的完整信息

select * from employee where salary > 10000 and sex = '男';

結果:
在這裏插入圖片描述
|---------------------------------------------------------------------------------------------------------------------

題6:篩選所有性別是男,或者薪資大於等於10000的員工的完整信息

select * from employee where salary >= 10000 or sex = '男';

結果:
在這裏插入圖片描述
|---------------------------------------------------------------------------------------------------------------------

題7(易錯題):篩選所有性別是男,或者薪資大於等於10000,或者薪資小於等於4000的員工的完整信息

select * from employee where sex = '男' and salary >= 10000 or salary <= 4000;

結果:出現了女性,原因是數據庫這樣處理:(sex = ‘男’ and salary >= 10000) or salary <= 4000,錯誤在於or的使用位置。夏在這裏插入圖片描述
改正:在後面兩個條件加上括號

select * from employee where sex = '男' and (salary >= 10000 or salary <= 4000);

正確結果;
在這裏插入圖片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章