您好,登錄后才能下訂單哦!
這篇文章主要講解了如何使用mysql連接查詢、聯合查詢、子查詢,內容清晰明了,對此有興趣的小伙伴可以學習一下,相信大家閱讀完之后會有幫助。
連接查詢:
-- 實驗表結構 create table student( id int, name varchar(15), gender varchar(15), cid int ); create table class( cid int, cname varchar(15) ); drop table student,class; -- 實驗表數據: insert into student values(1,"lilei","male",1),(2,"hanmeimei","male",2),(3,"jack","male",1),(4,"alice","female",4); --這里特意創建了一個class中沒有的4 insert into class values(1,"linux"),(2,"python"),(3,"java"),(5,"html5");--這里特意創建了一個student中沒有的5 select * from student; select * from class;
-- 內連接 -- select * from student inner join class; --結果與交叉連接相同 select * from student join class on student.cid = class.cid; select * from student inner join class on student.cid = class.cid;
select * from student left join class on student.cid = class.cid;
select * from student right join class on student.cid = class.cid;
select * from student natural join class;
select * from student natural left join class;
select * from student cross join class; select * from student,class;
select * from student inner join class on student.cid = class.cid;-- 原本結果 select id,name,gender,c.cid,cname from student as s inner join class as c on s.cid = c.cid;-- 使用表別名
select name,gender from student union select * from class; -- 因為class就兩個字段,所以第一個只選出兩個字段
-- 這是一個無意義的例子。僅為舉例使用 select cid,cname from (select * from class where cname="python") as c;
select * from student where cid=(select cid from class where cname="python");
-- 這是一個如果學生沒有選擇cid=1的課,那么不輸出對應課程信息的例子 select * from class where exists(select * from student where cid=1) and cid=1;
看完上述內容,是不是對如何使用mysql連接查詢、聯合查詢、子查詢有進一步的了解,如果還想學習更多內容,歡迎關注億速云行業資訊頻道。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。