参考 Table 1 中的 UID,计数 Table 2 个实体

Count Table 2 entities with reference to the UID in Table 1

我正在尝试参考另一个 table 中的 ID 找出某些条目的计数。不幸的是,我不具备实际找到解决方案的专业知识,因为我对 PHP 还很陌生。请参考下面,因为我认为这比我解释的更清楚:

TABLE 1:(重复table)(这是它在网页上的样子)

Unique ID | product name | Bid Count | Min Bid
-------------------------------------------------
01        | Product A    |   (4)     |    00  
02        | Product B    |   (6)     |    0  

Table 2:(第二个 table 收集买家的出价,但唯一 ID 与 Table 1 相同)

-------------------------------------------------
Unique ID | product name | Bid ($) 
-------------------------------------------------
01        | Product A    |  00  
01        | Product A    |  00  
01        | Product A    |  00  
01        | Product A    |  00  <<Lowest Bid | 4 bids count >>
--------------------------------------------------
02        | Product B    |  00
02        | Product B    |  00
02        | Product B    |  0
02        | Product B    |  0
02        | Product B    |  0
02        | Product B    |  0  <<Lowest Bid | 6 bids count >>

请帮助完成这个挑战

考虑以下 -

mysql> create table table1 (id int, product_name varchar(100));
Query OK, 0 rows affected (0.11 sec)

mysql> insert into table1 values (1,'Prod A'),(2,'Prod B');
Query OK, 2 rows affected (0.03 sec)
Records: 2  Duplicates: 0  Warnings: 0

mysql> create table table2 (id int, bid int);
Query OK, 0 rows affected (0.10 sec)

mysql> insert into table2 values (1,2000),(1,1500),(1,1200),(1,1000),(2,1500),(2,1000),(2,700),(2,800),(2,600),(2,500);
Query OK, 10 rows affected (0.03 sec)
Records: 10  Duplicates: 0  Warnings: 0

mysql> select * from table1;
+------+--------------+
| id   | product_name |
+------+--------------+
|    1 | Prod A       |
|    2 | Prod B       |
+------+--------------+
2 rows in set (0.00 sec)

mysql> select * from table2;
+------+------+
| id   | bid  |
+------+------+
|    1 | 2000 |
|    1 | 1500 |
|    1 | 1200 |
|    1 | 1000 |
|    2 | 1500 |
|    2 | 1000 |
|    2 |  700 |
|    2 |  800 |
|    2 |  600 |
|    2 |  500 |
+------+------+
10 rows in set (0.00 sec)

现在您可以使用 join 和聚合函数 countmin 以及最后的 group by

获得所需的结果
select t1.id , 
t1.product_name, 
count(t2.id) as `Bid Count`,
min(t2.bid) as `Min Bid` 
from table1 t1 join table2 t2 on t1.id = t2.id 
group by t1.id ;