########################多表########################
SELECT COUNT(*) FROM MEMBER1 A;
查询出来的结果为43行数据;
SELECT COUNT(*) FROM LOAN B;
查询出来的结果为13行数据;
#笛卡尔集 A*B,就是说A表的行数与B表的行数相乘所得到的结果,43*13=559行,下面2条语句的结果一样
SELECT COUNT(*) FROM MEMBER1 A,LOAN B;
SELECT * FROM MEMBER1 A INNER JOIN LOAN B;
#隐式内连接,查询匹配条件的结果,13行数据;
SELECT A.ID,A.REGNAME,A.LEAVEAMOUNT,B.ID,B.TITLE,B.AMOUNT FROM MEMBER1 A,LOAN B WHERE A.ID = B.MEMBERID;
#内连接和外连接的区别:
#内连接是查询所有匹配的数据
#外连接主表的数据都会显示,从表的数据匹配则显示
#显示内连接 ON关注的是表如何关联,WHERE关注结果的筛选,如下图所示:
不带where 条件情况:
SELECT A.ID,A.REGNAME,A.LEAVEAMOUNT,B.ID,B.MEMBERID,B.TITLE,B.AMOUNT FROM MEMBER1 A INNER JOIN LOAN B ON A.ID = B.MEMBERID;
带where 条件情况:
#左外连接,左表是主表,右表是从表
SELECT A.ID,A.REGNAME,A.LEAVEAMOUNT,B.ID,B.TITLE,B.AMOUNT
FROM MEMBER1 A LEFT JOIN LOAN B ON B.MEMBERID = A.ID;
#右外连接,左表是从表,右表是主表
SELECT A.ID,A.REGNAME,A.LEAVEAMOUNT,B.ID,B.TITLE,B.AMOUNT
FROM MEMBER1 A RIGHT JOIN LOAN B ON B.MEMBERID = A.ID;
#左外连接查询不匹配的数据
SELECT A.ID,A.REGNAME,A.LEAVEAMOUNT,B.ID,B.TITLE,B.AMOUNT
FROM LOAN B LEFT JOIN MEMBER1 A ON B.MEMBERID = A.ID WHERE A.ID IS NULL ;