java學習筆記之mysql3
阿新 • • 發佈:2017-12-31
style log 同名 ner mysql學習 mys 多表查詢 什麽 pid
mysql學習筆記之多表查詢和子查詢
1 #--------------多表的查詢------------------- 2 # 交叉連接查詢 3 # 內連接查詢 4 #顯式內連接 5 #隱式內連接 6 # 外鏈接查詢 7 #左外連接 8 #右外連接 9 10 #-----------交叉連接(了解)------------------------- 11 # select * from A, B; ----> A * B --->笛卡爾集合(冗余) 12 # 後邊的查詢基本上都是在笛卡爾集的基礎上進行條件判斷13 #查詢每一個分類都有哪些商品:電子 --->商品 服裝--->商品 14 SELECT * FROM category,products; 15 FOR(){ 16 FOR(){ 17 行 * 行 18 } 19 } 20 21 22 #---------內連接(重要) 隱式內連接---------------------------------- 23 #內連接求的是兩張表的交集 ,如果多張表沒有交集,則什麽都不顯示 24 #隱式內連接在判斷條件之前會產生笛卡爾集 25 #顯式內連接在判斷條件之前不會產生笛卡爾集26 FOR(){ 27 FOR(){ 28 IF(cid == category_id){ 29 行 * 行 30 } 31 } 32 } 33 34 SELECT * FROM category,products WHERE cid = category_id; 35 SELECT * FROM products,category WHERE cid = category_id; 36 37 #為了避免多個表出現同名的列,則在查詢時,一般要用表來訪問列 38 SELECT * FROM category,products WHEREcategory.cid = products.category_id; 39 40 #給表起別名:不能加引號,要字段起別名,加引號 41 SELECT * FROM category c,products p WHERE c.cid = p.category_id; 42 43 SELECT c.cname,p.pname FROM category c,products p WHERE c.cid = p.category_id; 44 45 #查詢省表和市表(一對多) 46 SELECT * FROM province p, city c WHERE p.pid = c.province_id; 47 #只顯示河南省的市 48 SELECT * FROM province p, city c WHERE p.pid = c.province_id AND p.pname=‘河南省‘; 49 SELECT * FROM province p, city c WHERE p.pid = c.province_id AND p.pname IN(‘河南省‘,‘山東省‘); 50 SELECT * FROM province p, city c WHERE p.pid = c.province_id AND (p.pname =‘河南省‘ OR p.pname=‘山東省‘); 51 52 #查詢用戶表和角色表(多對多) 53 SELECT * FROM USER u, user_role ur , role r WHERE u.uid = ur.uid AND ur.rid = r.rid; 54 SELECT * FROM USER u, user_role ur , role r,role_privilege rp, privilege p WHERE 55 u.uid = ur.uid AND ur.rid = r.rid AND r.rid = rp.rid AND rp.pid = p.pid; 56 57 #---------內連接(重要) 顯式內連接---------------------------------- 58 SELECT * FROM category c INNER JOIN products p ON c.cid = p.category_id WHERE c.cname=‘家電‘; 59 60 61 #--------外連接(左外連接)------ 62 #左外連接以左表為主,將左表的數據全部顯式,如果右表沒有對應的數據,則默認為NULL 63 SELECT * FROM category c INNER JOIN products p ON c.cid = p.category_id 64 SELECT * FROM category c LEFT OUTER JOIN products p ON c.cid = p.category_id 65 #--------外連接(右外連接)------ 66 #右外連接以右表為主,右表的數據全部顯式,左表沒有的數據默認為NULL 67 SELECT * FROM category c RIGHT OUTER JOIN products p ON c.cid = p.category_id 68 69 #---------------子查詢(重要)----------- 70 #子查詢就是select的嵌套 71 #在市表中查詢哪些市屬於河南省 72 #1 先在省表中查詢河南省的pid 73 SELECT pid FROM province WHERE pname = ‘河南省‘; 74 #2 在市表中查詢:province_id = pid 75 SELECT * FROM city WHERE province_id = (SELECT pid FROM province WHERE pname = ‘河南省‘); 76 77 #查詢河南省和山東省的市 78 SELECT * FROM city WHERE province_id IN (SELECT pid FROM province WHERE pname = ‘河南省‘ OR pname=‘山東省‘); 79 SELECT * FROM city WHERE (province_id = 80 (SELECT pid FROM province WHERE pname = ‘河南省‘)) OR (province_id = (SELECT pid FROM province WHERE pname = ‘山東省‘)); 81 82 #將select的查詢結果當成一個張表 83 #select查詢的結果是一張臨時表,存在內存中 84 SELECT * FROM city c , (SELECT * FROM province WHERE pname = ‘河南省‘) t WHERE c.province_id = t.pid; 85 86 87
java學習筆記之mysql3