【发布时间】:2018-03-08 14:43:10
【问题描述】:
我希望这些查询合二为一:
select count(first_column) as first from table1 where user_id = $id
select count(second_column) as second from table1 where user_id = $id
select count(first_column) as third from table2 where user_id = $id
我有这个:
select COUNT(table1.first_column) AS first,
COUNT(table1.second_column) AS second,
COUNT(table2.first_column) AS third
from table2 inner join
table2 on table1.user_id = table2.user_id
where table1.user_id = 1;
但它返回 table2 的错误值。 我该如何解决这个问题?
【问题讨论】:
-
你能给出示例数据、你的输出和想要的输出吗?
-
我关注并关注了 table1 和 table2 的帖子。我想计算用户关注、关注和帖子数。数据是这样的:user_id:1,follows:11,followed:null - user_id:1,follows:24,followed:null - user_id:1,follows:null,followed:54和posts表post_id:22,user_id :1 - post_id:26, user_id:1。当我通过 table2.post_id 添加组时,它返回正确的 table1 但不是 table2。 follow和followed是目标user_id的id,不是这里的count。