Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/231.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/66.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
计算第二个表中外键的数量并用第一个表中的行显示它的有效方法-PHP-MySQL_Php_Mysql - Fatal编程技术网

计算第二个表中外键的数量并用第一个表中的行显示它的有效方法-PHP-MySQL

计算第二个表中外键的数量并用第一个表中的行显示它的有效方法-PHP-MySQL,php,mysql,Php,Mysql,我有两个表tablea和tableb,如下所示 表a +--------+-------+-------+-------+------+ | fa | fb | fc | fd | fe | +--------+-------+-------+-------+------+ | col1 | f11 | f12 | f13 | x1 | +--------+-------+-------+-------+------+ | col2 | f

我有两个表
tablea
tableb
,如下所示

表a

+--------+-------+-------+-------+------+
|   fa   |   fb  |  fc   |  fd   |  fe  |
+--------+-------+-------+-------+------+
|  col1  |  f11  |  f12  |  f13  |  x1  |
+--------+-------+-------+-------+------+
|  col2  |  f21  |  f22  |  f23  |  x2  |
+--------+-------+-------+-------+------+
|  col3  |  f31  |  f32  |  f33  |  x3  |
+--------+-------+-------+-------+------+
|  col4  |  f41  |  f42  |  f43  |  x4  |
+--------+-------+-------+-------+------+
表B

+--------+-------+-------+-------+------+
|  tbba  |  tbbb |  tbbc |  tbbd | tbbe |
+--------+-------+-------+-------+------+
|  cola  |  fa1  |  fa2  |  fa3  |  x1  |
+--------+-------+-------+-------+------+
|  colb  |  fb1  |  fb2  |  fb3  |  x1  |
+--------+-------+-------+-------+------+
|  colc  |  fc1  |  fc2  |  fc3  |  x1  |
+--------+-------+-------+-------+------+
|  cold  |  fd1  |  fd2  |  fd3  |  x2  |
+--------+-------+-------+-------+------+
|  cole  |  fe1  |  fe2  |  fe3  |  x2  |
+--------+-------+-------+-------+------+
|  colf  |  ff1  |  ff2  |  ff3  |  x3  |
+--------+-------+-------+-------+------+
|  colg  |  fg1  |  fg2  |  fg3  |  x3  |
+--------+-------+-------+-------+------+
|  colh  |  fh1  |  fh2  |  fh3  |  x3  |
+--------+-------+-------+-------+------+
|  coli  |  fi1  |  fi2  |  fi3  |  x3  |
+--------+-------+-------+-------+------+
|  colj  |  fj1  |  fj2  |  fj3  |  x4  |
+--------+-------+-------+-------+------+
我想生成一个表,如下所示

+--------+-------+-----+
|  col1  |  f11  |  3  |
+--------+-------+-----+
|  col2  |  f21  |  2  |
+--------+-------+-----+
|  col3  |  f31  |  4  |
+--------+-------+-----+
|  col4  |  f41  |  1  |
+--------+-------+-----+
数字表示
tablea
fe
字段中的条目出现在
tableb
中的时间。目前,我正在从
tablea
获取所有行,在每个循环中,我使用
tablea
fe
字段使用另一个查询来获取
tableb
中的行数。因此,我有1个主查询和4个子查询。有没有什么有效的方法,比如加入什么的

谢谢

试试这个:

SELECT a.fa , a.fb , COUNT( b.tbbe) 
FROM  tablea a
LEFT JOIN tableb b ON a.fe = b.tbbe
GROUP BY a.fa 
试试这个:

SELECT a.fa , a.fb , COUNT( b.tbbe) 
FROM  tablea a
LEFT JOIN tableb b ON a.fe = b.tbbe
GROUP BY a.fa 

你能把fe=tbbe和group by fa,fb上的两个表连接起来吗?你能把fe=tbbe和group by fa,fb上的两个表连接起来吗?你能把fe=tbbe和group by fa,fb上的两个表连接起来吗?为什么我们使用
group by a.fa
?否则你只能得到一行。这是为了获得每个a.fa的计数。为什么我们使用
按a.fa分组
?否则您将只获得一行。这是为了获得每个a.fa的计数。为什么我们使用
按a.fa分组
?否则您将只获得一行。这是每个a.fa的计数。