0

我有两个数据表,比如表名

|ID | Name |
|1  | B1   |
|2  | B2   |

然后我有另一个名为 block_worker 的表

|block_id | Name | No Of Worker|
|1        | sam|
|1        | dave|
|2        |  Rob |

现在我想编写一个查询,显示来自块表的数据,并block worker根据block id.

结果将是这样的

|ID | Name | Worker
|1 | B1 |   2    |
|2 | B2 |   1    |
4

2 回答 2

1
select b.id, b.name, count(w.name) as Workers
from block b
left join block_worker w on w.block_id = b.id
group by b.id, b.name
于 2013-10-18T10:24:16.287 回答
1
Try Like this

    SELECT A.ID,A.Name,COUNT(B.block_id) AS Worker FROM block AS A JOIN block_worker AS B 
ON A.ID = B.block_id GROUP BY B.block_id
于 2013-10-18T10:26:45.167 回答