MySQL 다중 테이블 쿼리
1 다중 테이블 쿼리에 SELECT 절 사용
SELECT 필드 이름 FROM 테이블 1, 테이블 2... WHERE 테이블 1. 필드 = 테이블 2. field AND other 쿼리 조건
SELECT a.id,a.name,a.address,a.date,b.math,b.english,b.chinese FROM tb_demo065_tel AS b,tb_demo065 AS a WHERE a.id=b.id
참고: 위 코드에서는 두 테이블의 id 필드 정보가 동일하다는 조건을 기반으로 두 테이블 간의 연결이 설정됩니다. 실제 개발에서는 이러한 방식으로 사용하는 것이
을 달성하기 위해 기본 키와 외래 키를 사용하는 것이 가장 좋습니다. 2. 다중 테이블 쿼리에는
과 같은 테이블 별칭을 사용합니다. 🎜>
SELECT a.id,a.name,a.address,b.math,b.english,b.chinese FROM tb_demo065 a,tb_demo065_tel b WHERE a.id=b.id AND b.id='$_POST[textid]'
SELECT a.id,a.name,a.address,b.math,b.english,b.chinese FROM tb_demo065 AS a,tb_demo065_tel AS b WHERE a.id=b.id
SELECT a.id,a.name,a.address,b.math,b.english,b.chinese FROM tb_demo065 a,tb_demo065_tel b WHERE a.id=b.id
e.x:SELECT id,name,pwd FROM tb_demo067 UNION SELECT uid,price,date FROM tb_demo067_tel
SELECT id,name,sex,date FROM tb_demo068 WHERE id in(SELECT id FROM tb_demo068 WHERE id='$_POST[test]')
test_expression[NOT] IN{ subquery }
6 쿼리 통계에서 중첩 쿼리 적용 다중 테이블 쿼리를 구현할 때 ANY, SOME, ALL 조건자를 동시에 사용할 수 있으며 이러한 조건자를 정량적 비교 조건부라고 하며 결합할 수 있습니다. 비교 연산자와 함께 모든 반환 값이 검색 조건을 충족하는지 확인하는 데 사용됩니다. SOME 및 ANY 조건자는 존재하며 반환 값이 검색 조건을 충족하는지 여부에만 초점을 맞춥니다. ALL 술어는 검색 요구 사항을 충족하는 술어가 있는지 여부에만 관심이 있습니다.
SELECT * FROM tb_demo069_people WHERE uid IN(SELECT deptID FROM tb_demo069_dept WHERE deptName='$_POST[select]') SELECT a.id,a.name FROM tb_demo067 AS a WHERE id<3)
SELECT people.name,people.chinese,people.math,people.english FROM (SELECT name,chinese,math,english FROM tb_demo071) AS people
(1)由比较运算符引入的内层子查询只包含一个表达式或列名,在外层语句中的WHERE子句内命名的列必须与内层子查询命名的列兼容
(2)由不可更改的比较运算符引入的子查询(比较运算符后面不跟关键字ANY或ALL)不包括GROUP BY 或 HAVING子句,除非预先确定了成组或单个的值
(3)用EXISTS引入的SELECT列表一般都由*组成,不必指定列名
(4)子查询不能在内部处理其结果
八 使用子查询作表达式
SELECT (SELECT AVG(chinese)FROM tb_demo071),(SELECT AVG(english)FROM tb_demo071),(SELECT AVG(math)FROM tb_demo071) FROM tb_demo071
注:在使用子查询时最好为列表项取个别名,这样可以方便用户在使用mysql_fetch_array()函数时为表项赋值,如
SELECT (SELECT AVG(chinese) FROM tb_demo071) AS yuwen ,(SELECT AVG(english) FROM tb_demo071) AS yingyu,(SELECT AVG(math) FROM tb_demo071) AS shuxue FROM tb_demo071
九 使用子查询关联数据
SELECT * FROM tb_demo072_student WHERE id=(SELECT id FROM tb_demo072_class WHERE className = '$_POST[text]')
十 多表联合查询
利用SQL语句中的UNION,可以将不同表中符合条件的数据信息显示在同一列中。
e.x:SELECT * FROM tb_demo074_student UNION SELECT * FROM tb_demo074_fasten
注:使用UNION时应注意以下两点:
(1)在使用UNION运算符组合的语句中,所有选择列表的表达式数目必须相同,如列名、算术表达式及聚合函数等
(2)在每个查询表中,对应列的数据结构必须一样。
十一 对联合后的结果进行排序
为了UNION的运算兼容,要求所有SELECT语句都不能有ORDER BY语句,但有一种情况例外,那就是在最后一个SELECT语句中放置ORDER BY 子句实现结果的最终排序输出。
e.x:SELECT * FROM tb_demo074_student UNION SELECT * FROM tb_demo074_fasten ORDER BY id
使用UNION条件上相对比较苛刻,所以使用此语句时一定要注意两个表项数目和字段类型是否相同
十二 条件联合语句
SELECT * FROM tb_demo076_BEIJING GROUP BY name HAVING name='人民邮电出版社' OR name='机械工业出版社' UNION SELECT * FROM tb_demo076_BEIJING GROUP BY name HAVING name <>'人民邮电出版社' AND name <>'机械工业再版社' ORDER BY id
上面语句应用了GROUP BY分组语句和HAVING语句实现条件联合查询。其实现目的是先保证将'人民邮电出版社'和'机械工业出版社'始终位于名单最前列,然后再输出其它的出版社
十三 简单内连接查询
SELECT filedlist FROM table1 [INNER] JOIN table2 ON table1.column1 = table2.column1
其中,filedlist是要显示的字段,INNER表示表之间的连接方式为内连接,table1.column1=table2.column1用于指明两表间的连接条件,如:
SELECT a.name,a.address,a.date,b.chinese,b.math,b.english FROM tb_demo065 AS a INNER JOIN tb_demo065_tel AS b on a.id=b.id
十四 复杂内连接查询
复杂的内连接查询是在基本的内连接查询的基础上再附加一些查询条件,如:
SELECT a.name,a.address,a.date,b.chinese,b.math,b.english FROM tb_demo065 AS a INNER JOIN tb_demo065_tel AS b on a.id=b.id WHERE b.id=(SELECT id FROM tb_demo065 WHERE tb_demo065.name='$_POST[text]')
总之,实现表与表之间的关联的本质是两表之间存在共同的数据项或者相同的数据项,通过WHERE 子句或内连接INNER JOIN … ON 语句将两表连接起来,实现查询
十五 使用外连接实现多表联合查询
(1)LEFT OUTER JOIN表示表之间通过左连接方式相互连接,也可简写成LEFT JOIN,它是以左侧的表为基准故称左连接,左侧表中所有信息将被全部输出,而右侧表信息则只会输出符合条件的信息,对不符合条件的信息则返回NULL
e.x:SELECT a.name,a.address,b.math,b.english FROM tb_demo065 AS A LEFT OUTER JOIN tb_demo065_tel AS b ON a.id=b.id
(2)RIGHT OUTER JOIN表示表之间通过右连接方式相互连接,也可简写成RIGHT JOIN,它是以右侧的表为基准故称右连接,右侧表中所有信息将被全部输出,而左侧表信息则只会输出符合条件的信息,对不符合条件的信息则返回NULL
E.X:SELECT a.name,a.address,b.math,b.english FROM tb_demo065 AS A RIGHT OUTER JOIN tb_demo065_tel AS b ON a.id=b.id
十六 利用IN或NOTIN关键字限定范围
e.x:SELECT * FROM tb_demo083 WHERE code IN(SELECT code FROM tb_demo083 WHERE code BETWEEN '$_POST[text1]' AND '$_POST[text2]')
利用IN可指定在范围内查询,若要求在某范围外查询可以用NOT IN代替它
十七 由IN引入的关联子查询
e.x:SELECT * FROM tb_demo083 WHERE code IN(SELECT code FROM tb_demo083 WHERE code = '$_POST[text]')
十八 利用HAVING语句过滤分组数据
HAVING子句用于指定组或聚合的搜索条件,HAVING通常与GROUP BY 语句一起使用,如果SQL语句中不含GROUP BY子句,则HAVING的行为与WHERE子句一样.
e.x:SELECT name,math FROM tb_demo083 GROUP BY id HAVING math > '95'