java学习笔记之mysql3
时间:2021-07-01 10:21:17
帮助过:7人阅读
#
--------------多表的查询-------------------
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
WHERE category.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
标签:交叉连接 user ali sql select查询 role span 查询 哪些