這篇文章主要講解了“SQL語言的查詢方法有哪些”,文中的講解內容簡單清晰,易于學習與理解,下面請大家跟著小編的思路慢慢深入,一起來研究和學習“SQL語言的查詢方法有哪些”吧!
MySQL屬于關系型的數據庫,表之間可以建立關系,如:學生表和成績表,在成績表中添加學生編號引用學生表中的學生編號,這樣在成績表中就不用添加重復的學生信息了,這種關系也叫主外鍵關系,可以通過設置外鍵約束實現。
可以在創建表時,添加外鍵約束來保證表和表之間引用完整性,添加外鍵后:
在插入外鍵表數據前,必須先插入主表數據
在刪除主表數據前,必須先刪除外鍵表數據
語法:
create table 表名
(
字段名 類型 約束,
... ,
constraint 外鍵名稱 foreign key (外鍵列) references 主表(主鍵)
);
代碼示例:
use mysql_db;
-- 創建成績表
drop table if exists tb_score;
create table tb_score
(
score_id int primary key auto_increment,
score_stu_id int,
score int,
score_course varchar(20),
constraint fk_score_stu_id foreign key(score_stu_id) references tb_student(stu_id)
);
在查詢時我們經常要把相關的多張表的字段,一起查詢出來,如查詢學生成績時,要顯示分數和學生姓名。這個時候我們就需要連接查詢了,連接查詢分為內連接和外連接,我們先學習內連接查詢。
內連接查詢的特點是:會查詢出相關表中都存在的數據。
語法有兩種實現方法:
1)select 字段..... from 表1 inner join 表2
on 表1.主鍵 = 表2.外鍵;
注意:這里假設表1是主表,內連接表的前后順序無關
2)select 字段..... from 表1 , 表2
where 表1.主鍵 = 表2.外鍵;
代碼示例:
-- 查詢學生姓名和成績 方式1
select s.stu_id ,s.stu_name,c.score_course,c.score from tb_score c inner join tb_student s on s.stu_id = c.score_stu_id;
-- 方式2
select s.stu_id ,s.stu_name,c.score_course,c.score from tb_score c , tb_student s where s.stu_id = c.score_stu_id;
效果相同:
cdn.xitu.io/2019/6/5/16b265444d53bca3?w=300&h=196&f=png&s=35605">
外連接分為左外連接和右外連接:
1) 左外連接
連接查詢多張表的數據,顯示所有左表的數據,右表存在不相符的數據補null。
語法:
select 字段... from 左表 left join 右表
on 主表.主鍵 = 子表.外鍵;
代碼示例:
-- 左外連接,查詢學生姓名和成績
select s.stu_id,s.stu_name,c.score_course,c.score from tb_student s left join tb_score c on s.stu_id = c.score_stu_id;
-- 查詢所有參加過考試的同學
select s.stu_id,s.stu_name,c.score_course,c.score from tb_student s left join tb_score c on s.stu_id = c.score_stu_id where c.score is not null;
-- 查詢所有沒參加過考試的同學
select s.stu_id,s.stu_name,c.score_course,c.score from tb_student s left join tb_score c on s.stu_id = c.score_stu_id where c.score is null;
2)右外連接
與左連接相反,顯示所有右表數據,左表中不相符的數據補null。
語法:
select 字段... from 左表 right join 右表
on 主表.主鍵 = 子表.外鍵;
代碼示例:
select s.stu_id,s.stu_name,c.score_course,c.score from tb_score c right join tb_student s
on s.stu_id = c.score_stu_id;
在查詢語句中還可以嵌入查詢語句,嵌入的查詢也叫子查詢,子查詢將先執行,查詢到結果后,父查詢可以將此結果作為查詢條件再進行一次查詢,這樣可以解決比較復雜的查詢問題。
語法:
select ... from 表 where 字段 比較運算符 (select ... from 表 where 條件);
代碼示例:
-- 查詢年齡比李四大的學生
select * from tb_student where stu_age > (select stu_age from tb_student where stu_name = '李四');
-- 查詢李四的老鄉
select * from tb_student where stu_address = (select stu_address from tb_student where stu_name = '李四');
有時候當子查詢中查詢結果不止一個的情況下,使用比較運算符會出現錯誤,這時候我們就需要使用一些關鍵字來幫助篩選結果。
in關鍵字的作用是在字段和數據列表中任意一個相等,條件就成立。
代碼示例:
-- 查詢語文分數考相同的學生,先用子查詢查語文的成績,在用內連接查考過語文的學生姓名和成績,把成績進行比較
select stu_name,score_course,score from tb_student inner join tb_score on tb_student.stu_id = tb_score.score_stu_id where score_course='語文' and score in(select score from tb_score where score_course = '語文');
all和比較運算符配合使用,如果字段和所有的查詢結果都比較成立,結果才成立。
語法:
字段 比較運算 all(查詢結果)
代碼示例:
-- 查詢比所有男學生小的女學生,先查所有男學生的年齡,如果女學生年齡比所有這些年齡大,就查出來
select stu_name,stu_age,stu_gender from tb_student where stu_gender = '女' and stu_age < all(select stu_age from tb_student where stu_gender = '男');
any和比較運算符配合使用,如果字段和任意一個查詢結果比較成立,則結果成立。
語法:
字段 比較運算 any(查詢結果)
代碼示例:
-- 查詢只要比一個南京學生大的武漢學生
select stu_name,stu_address from tb_student where stu_address = '武漢'
and stu_age > any(select stu_age from tb_student where stu_address='南京');
exists表示是否有查詢結果,如果沒有結果,返回false,有結果則返回true
語法:
exists(查詢結果)
-- 查詢考過英語的同學,在子查詢中需要判斷父查詢中的學生id是否在子查詢中存在
select * from tb_student where
exists(select score_stu_id from tb_score where tb_student.stu_id = tb_score.score_stu_id and score_course = '英語');
感謝各位的閱讀,以上就是“SQL語言的查詢方法有哪些”的內容了,經過本文的學習后,相信大家對SQL語言的查詢方法有哪些這一問題有了更深刻的體會,具體使用情況還需要大家實踐驗證。這里是億速云,小編將為大家推送更多相關知識點的文章,歡迎關注!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。