MySQL 如何多表查询

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了MySQL 如何多表查询相关的知识,希望对你有一定的参考价值。

表a
id, nid, price, time

表b
id, uid, nid, node, time

统计两个表的行数,条件是:相同的一个nid

我目前使用的方法是:

先查表a:select * from `a` where `nid` = '123';
$row = mysql_num_rows(SQL);

再查表b:select * from `b` where `nid` = '123';
$row2 = mysql_num_rows(SQL);

$count = $row+$row2;

本人新手,学习中知道有多表查询,求教,

求SQL

我觉得:

    表结构不同的话,用你的方法比较好,速度快。

    写成这样:select count(*) from `b` where `nid` = '123';

    直接统计出数目了

 2.表结构相同的话:

    写成这样select count(*) from `a` where `nid` = '123' UNION ALL select count(*) from             `b` where `nid` = '123';

参考技术A SELECT SUM(num) FROM (SELECT COUNT(*) as num FROM a WHERE nid = '123' UNION SELECT COUNT(*) as num FROM b WHERE nid='123') as temp;

不过不建议如此使用····用简单的sql反而还快一点··追问

?

你是说我自己的那个方法(分别查询一次)还要快吗?

追答

表多的话有可能···你可以用代码测试一下···

参考技术B 1:建议将两张表合成一张表,合并好parentid. 加个key搜索字段,非常方便快捷。
2:inner join这是MYSQL多表查询语句。

MySQL 子查询与多表联合查询

子查询:就是一个查询的结果作为另一个查询的数据源或者条件。

如何查询出工资最大的人的姓名?

mysql> select max(sal) from emp;   --查询出工资最大值
+----------+
| max(sal) |
+----------+
|  5000.00 |
+----------+
1 row in set (0.00 sec)

mysql> select ename,sal from emp where sal = (select max(sal) from emp);--可以将工资的最大值最为一个筛选条件,select ename,sal from emp执行完之后赋值给sal sal = (select max(sal) from emp);
+-------+---------+
| ename | sal     |
+-------+---------+
| KING  | 5000.00 |
+-------+---------+
1 row in set (0.32 sec)

mysql> select ename,sal from emp where sal = (select min(sal) from emp);--查询最小工资
+-------+--------+
| ename | sal    |
+-------+--------+
| SMITH | 800.00 |
+-------+--------+
1 row in set (0.00 sec)

子查询情况;将查询结果作为另一个查询的条件

查询出公司都有那些人是经理人;看那些对应的empno出现在mgr字段中,谁就是经理人

mysql>  select mgr from emp;   查询
+------+
| mgr  |
+------+
| 7902 |
| 7698 |
| 7698 |
| 7839 |
| 7698 |
| 7839 |
| 7839 |
| 7566 |
| NULL |
| 7698 |
| 7788 |
| 7698 |
| 7566 |
| 7782 |
| NULL |
+------+
15 rows in set (0.00 sec)

mysql> select distinct mgr from emp;   --去重
+------+
| mgr  |
+------+
| 7902 |
| 7698 |
| 7839 |
| 7566 |
| NULL |
| 7788 |
| 7782 |
+------+
7 rows in set (0.00 sec)

mysql>  select distinct mgr from emp where mgr is not null;    --去掉null空值,通过这条语句得到的信息就是都有哪些empno出现在mgr字段中
+------+
| mgr  |
+------+
| 7902 |
| 7698 |
| 7839 |
| 7566 |
| 7788 |
| 7782 |
+------+
6 rows in set (0.01 sec)
mysql> select ename,empmo from emp where empmo in(select distinct mgr from emp);--查询公司都有哪些人是经理人in关键字
+-------+-------+
| ename | empmo |
+-------+-------+
| JONES |  7566 |
| BLAKE |  7698 |
| CLARK |  7782 |
| SCOTT |  7788 |
| KING  |  7839 |
| FORD  |  7902 |
+-------+-------+
6 rows in set (0.00 sec)
mysql> select ename,empmo from emp where empmo in(select distinct mgr from emp where mgr is not null);
+-------+-------+
| ename | empmo |
+-------+-------+
| JONES |  7566 |
| BLAKE |  7698 |
| CLARK |  7782 |
| SCOTT |  7788 |
| KING  |  7839 |
| FORD  |  7902 |
+-------+-------+
6 rows in set (0.00 sec)

注意:以上这两种情况都是将整个查询的一个结果作为另一个查询的条件,作为条件的时候这里用到了等值比较、in,其他的<><=>=都是可以的。查出平均工资最大的部门是那个?

1、每个部门的平均工资需要先查出来,组函数不可以嵌套

查询结果作为另一个查询的数据源,可以当成一张表。

注意:当成表的过程中必须起别名

mysql> select avg(sal),deptno from emp group by deptno;--查询平均工资和对应的部门编号
+-------------+--------+
| avg(sal)    | deptno |
+-------------+--------+
| 2356.540000 |   NULL |
| 2916.666667 |     10 |
| 2175.000000 |     20 |
| 1566.666667 |     30 |
+-------------+--------+
4 rows in set (0.00 sec)    

MySQL多表联合查询

查询员工的姓名和员工所在部门的名称 

mysql>  select ename,DNAME from emp,dept;
+------------+------------+
| ename      | DNAME      |
+------------+------------+
| SMITH      | ACCOUNTING |
| SMITH      | RESEARCH   |
| SMITH      | SALES      |
| SMITH      | OPERATIONS |
| ALLEN      | ACCOUNTING |
| ALLEN      | RESEARCH   |
| ALLEN      | SALES      |
| ALLEN      | OPERATIONS |
| WARD       | ACCOUNTING |
| WARD       | RESEARCH   |
| WARD       | SALES      |
| WARD       | OPERATIONS |
| JONES      | ACCOUNTING |
| JONES      | RESEARCH   |
| JONES      | SALES      |
| JONES      | OPERATIONS |
| MARTIN     | ACCOUNTING |
| MARTIN     | RESEARCH   |
| MARTIN     | SALES      |
| MARTIN     | OPERATIONS |
| BLAKE      | ACCOUNTING |
| BLAKE      | RESEARCH   |
| BLAKE      | SALES      |
| BLAKE      | OPERATIONS |
| CLARK      | ACCOUNTING |
| CLARK      | RESEARCH   |
| CLARK      | SALES      |
| CLARK      | OPERATIONS |
| SCOTT      | ACCOUNTING |
| SCOTT      | RESEARCH   |
| SCOTT      | SALES      |
| SCOTT      | OPERATIONS |
| KING       | ACCOUNTING |
| KING       | RESEARCH   |
| KING       | SALES      |
| KING       | OPERATIONS |
| TURNER     | ACCOUNTING |
| TURNER     | RESEARCH   |
| TURNER     | SALES      |
| TURNER     | OPERATIONS |
| ADAMS      | ACCOUNTING |
| ADAMS      | RESEARCH   |
| ADAMS      | SALES      |
| ADAMS      | OPERATIONS |
| JAMES      | ACCOUNTING |
| JAMES      | RESEARCH   |
| JAMES      | SALES      |
| JAMES      | OPERATIONS |
| FORD       | ACCOUNTING |
| FORD       | RESEARCH   |
| FORD       | SALES      |
| FORD       | OPERATIONS |
| MILLER     | ACCOUNTING |
| MILLER     | RESEARCH   |
| MILLER     | SALES      |
| MILLER     | OPERATIONS |
|   zhang    | ACCOUNTING |
|   zhang    | RESEARCH   |
|   zhang    | SALES      |
|   zhang    | OPERATIONS |
+------------+------------+
60 rows in set (0.29 sec)

笛卡尔积

(14*4)emp的每一条数据与dept表的每一条数据组合。也就是56条数据。

 多表联合查询

两个表中的deptno是相等的

mysql>  select ename,DNAME from emp,dept where emp.deptno = dept.deptno;
+--------+------------+
| ename  | DNAME      |
+--------+------------+
| SMITH  | RESEARCH   |
| ALLEN  | SALES      |
| WARD   | SALES      |
| JONES  | RESEARCH   |
| MARTIN | SALES      |
| BLAKE  | SALES      |
| CLARK  | ACCOUNTING |
| SCOTT  | RESEARCH   |
| KING   | ACCOUNTING |
| TURNER | SALES      |
| ADAMS  | RESEARCH   |
| JAMES  | SALES      |
| FORD   | RESEARCH   |
| MILLER | ACCOUNTING |
+--------+------------+
14 rows in set (0.01 sec)

  

关键字join= 联和 on = 条件;查询ename和dname在emp表和dept表查询,查询对应的数据on emp.deptno = dept.deptno

mysql> select ename,dname from emp join dept on emp.deptno = dept.deptno;
+--------+------------+
| ename  | dname      |
+--------+------------+
| SMITH  | RESEARCH   |
| ALLEN  | SALES      |
| WARD   | SALES      |
| JONES  | RESEARCH   |
| MARTIN | SALES      |
| BLAKE  | SALES      |
| CLARK  | ACCOUNTING |
| SCOTT  | RESEARCH   |
| KING   | ACCOUNTING |
| TURNER | SALES      |
| ADAMS  | RESEARCH   |
| JAMES  | SALES      |
| FORD   | RESEARCH   |
| MILLER | ACCOUNTING |
+--------+------------+
14 rows in set (0.00 sec)

查询某个人的姓名、所在的部门、工资的等级

mysql> select ename,DNAME,GRADE from emp join dept join salgrade on emp.deptno = dept.deptno and emp.sal between losal and hisal;
+--------+------------+-------+
| ename  | DNAME      | GRADE |
+--------+------------+-------+
| SMITH  | RESEARCH   |     1 |
| ADAMS  | RESEARCH   |     1 |
| JAMES  | SALES      |     1 |
| WARD   | SALES      |     2 |
| MARTIN | SALES      |     2 |
| MILLER | ACCOUNTING |     2 |
| ALLEN  | SALES      |     3 |
| TURNER | SALES      |     3 |
| JONES  | RESEARCH   |     4 |
| BLAKE  | SALES      |     4 |
| CLARK  | ACCOUNTING |     4 |
| SCOTT  | RESEARCH   |     4 |
| FORD   | RESEARCH   |     4 |
| KING   | ACCOUNTING |     5 |
+--------+------------+-------+
14 rows in set (0.01 sec)

  

 

以上是关于MySQL 如何多表查询的主要内容,如果未能解决你的问题,请参考以下文章

mysql 数据操作 多表查询 目录

MySql多表查询

MySQL-04-笔记

mysql 多表联合查询啥用

MySQL数据库语法-多表查询练习一

MySQL 子查询与多表联合查询