gpt4 book ai didi

php - 计算第二个表中的外键数量并将其与第一个表中的行一起显示的有效方法 - PHP - MySQL

转载 作者:行者123 更新时间:2023-11-30 22:42:25 24 4
gpt4 key购买 nike

我有两个表 tableatableb 如下;

表格

+--------+-------+-------+-------+------+
| 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 |
+--------+-------+-------+-------+------+

表格

+--------+-------+-------+-------+------+
| 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 |
+--------+-------+-----+

该数字表示tableafe 字段中的条目在tableb 中出现的次数。目前,我正在从 tablea 中获取所有行,并且在每个循环中,我使用另一个查询来获取 tableb 中的行数,使用 tableafe 字段。因此,我有 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

关于php - 计算第二个表中的外键数量并将其与第一个表中的行一起显示的有效方法 - PHP - MySQL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30746838/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com