Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/59.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_Join_Count - Fatal编程技术网

Php mysql连接:获取每种类型的部件数量

Php mysql连接:获取每种类型的部件数量,php,mysql,join,count,Php,Mysql,Join,Count,我有两张桌子: 1) 零件 2) 职位 在posts表中,post有三种类型之一,因此在type列I中输入值(1、2或3) 我想得到这样的表(包括count和join) part_id | part_name | count_posts_type1 | count_posts_type2 | count_posts_type3 如何使用mysql实现这一点 例如: 你有没有尝试过类似(只是开玩笑,我知道你什么都没试过)的东西,比如: SELECT parts.part_id,

我有两张桌子:

1) 零件

2) 职位

在posts表中,post有三种类型之一,因此在type列I中输入值(1、2或3)

我想得到这样的表(包括count和join)

part_id | part_name | count_posts_type1 | count_posts_type2 | count_posts_type3
如何使用mysql实现这一点

例如:


你有没有尝试过类似(只是开玩笑,我知道你什么都没试过)的东西,比如:

SELECT
     parts.part_id,
     parts.part_name,
     COUNT(t1.post_id) AS count_posts_type1,
     COUNT(t2.post_id) AS  count_posts_type2,
     COUNT(t3.post_id) AS count_posts_type3
FROM posts
     RIGHT JOIN part ON posts.part_id=parts.part_id 
     INNER JOIN ( SELECT post_id FROM posts WHERE type=1 ) as t1(post_id) ON posts.post_id = t1.post_id
     INNER JOIN ( SELECT post_id FROM posts WHERE type=1 ) as t2(post_id) ON posts.post_id = t2.post_id
     INNER JOIN ( SELECT post_id FROM posts WHERE type=1 ) as t3(post_id) ON posts.post_id = t3.post_id

如果类型列表有限,则使用sum和case-when是一种相当常见的方法

然后是一个左连接,对没有post的部分有结果(和一个合并,在和上有0而不是null),现在我们开始

select 
pa.part_id, pa.part_name,
coalesce(sum(case when po.type = 1 then 1 else 0 end), 0) as count_posts_type1,
coalesce(sum(case when po.type = 2 then 1 else 0 end), 0) as count_posts_type2,
coalesce(sum(case when po.type = 3 then 1 else 0 end), 0) as count_posts_type3
from parts pa
left join posts po on po.part_id = pa.part_id
group by pa.part_id, pa.part_name

这两个表之间的共同点是什么?共同点是part_id I fixed itI为我需要的结果添加了一个示例
SELECT
     parts.part_id,
     parts.part_name,
     COUNT(t1.post_id) AS count_posts_type1,
     COUNT(t2.post_id) AS  count_posts_type2,
     COUNT(t3.post_id) AS count_posts_type3
FROM posts
     RIGHT JOIN part ON posts.part_id=parts.part_id 
     INNER JOIN ( SELECT post_id FROM posts WHERE type=1 ) as t1(post_id) ON posts.post_id = t1.post_id
     INNER JOIN ( SELECT post_id FROM posts WHERE type=1 ) as t2(post_id) ON posts.post_id = t2.post_id
     INNER JOIN ( SELECT post_id FROM posts WHERE type=1 ) as t3(post_id) ON posts.post_id = t3.post_id
select 
pa.part_id, pa.part_name,
coalesce(sum(case when po.type = 1 then 1 else 0 end), 0) as count_posts_type1,
coalesce(sum(case when po.type = 2 then 1 else 0 end), 0) as count_posts_type2,
coalesce(sum(case when po.type = 3 then 1 else 0 end), 0) as count_posts_type3
from parts pa
left join posts po on po.part_id = pa.part_id
group by pa.part_id, pa.part_name