mysql模糊查詢語句_Mysql查詢語句練習
阿新 • • 發佈:2020-12-10
技術標籤:mysql模糊查詢語句mysql練習
一. 練習用表展示
Student
Course
Score
teacher
二.練習
1.select查詢
展示查詢語句及結果
-- 查詢學生表
-- 查詢學生表所有資訊
select *
from student;
-- 查詢姓名,性別並自定義別名(as的用法)
select 姓名 as s_name,性別 as '人類性別'
from student;
-- 查詢不重複姓名(distinct用法) select DISTINCT 姓名 from student;
-- 查詢多列不重複(姓名 學號)
select DISTINCT 學號,姓名
from student;
2.算術運算子查詢練習
展示查詢語句及結果
-- 使用運算子查詢
-- 求成績百分比
SELECT 學號,成績,成績/100 as '百分比成績'
from score;
-- 比較運算子號(=)
SELECT 姓名,學號
from student
where 姓名='猴子';
-- 查詢NULL值
select 教師號,教師姓名
from teacher
where 教師姓名 is NULL;
-- 查詢非null值 select 教師號,教師姓名 from teacher where 教師姓名 is not NULL;
3.邏輯運算子查詢練習
展示查詢語句及結果
-- 邏輯運算子not
SELECT 學號,成績
FROM score
where not 成績>=60;-- 成績小於60
-- 邏輯運算子and
select 學號,成績
from score
where 成績>=60 and 成績<=90; -- 成績 between 60 and 90;
-- 邏輯運算子or
select 姓名,性別
from student
where 性別='男'
and (姓名='馬雲'or 姓名= '猴子');-- 性別男,姓名為馬雲或者猴子
-- 範圍查詢between select 學號,成績 from score where 成績 between 60 and 90;
select 學號,成績
from score
where 成績 <60 OR 成績 >90;-- 小於60,大於90的成績都會被選
-- in 的用法
select 姓名,性別
from student
where 性別='男'
and 姓名 in ('馬雲', '猴子');
-- not in
select 姓名,性別
from student
where 性別='男'
and 姓名 not in ('馬雲', '猴子');
4.模糊查詢練習
展示查詢語句及結果
-- 查詢姓猴的學生名單
select *
from student
where 姓名 like '猴%';
-- 查詢名字最後一個字是猴的學生名單
select *
from student
where 姓名 like '%猴';
-- 查詢姓名中帶猴的名字的學生名單
SELECT*
FROM student
WHERE 姓名 like'%猴%';