我需要在mysql中连接两个表
我需要一些帮助来解决我的问题。我想连接两个select语句的输出: 第一 及其产出我需要在mysql中连接两个表,mysql,database,Mysql,Database,我需要一些帮助来解决我的问题。我想连接两个select语句的输出: 第一 及其产出 Year Count 2014 18 2015 117 2016 9 Count 18 110 11 第二个问题 select count(extract(year from createdDate)) as count from table where userId=322 group by extract(year from createdDate);
Year Count
2014 18
2015 117
2016 9
Count
18
110
11
第二个问题
select count(extract(year from createdDate)) as count
from table
where userId=322
group by extract(year from createdDate);
及其产出
Year Count
2014 18
2015 117
2016 9
Count
18
110
11
我想将这两个表添加到一个表中。
我想要那种类型的输出
Year Count Count
2014 18 18
2015 117 110
2016 9 11
请注意,我在查询1中使用来搜索用户id
,但在查询2中使用用户id
我试图解决这个问题,但在输出中得到了重复的值。
有人知道这个解决方案吗?将它们写成子查询,并将它们连接在一起
SELECT a.year, a.count AS t_user_count, b.count AS user_count
FROM (select YEAR(create_date) AS year, COUNT(*) AS count
FROM table
WHERE to_user_id = 322
GROUP BY year) AS a
JOIN (SELECT YEAR(create_date) AS year, COUNT(*) AS count
FROM table
WHERE user_id = 322
GROUP BY year) AS b
ON a.year = b.year
为什么这两个查询对同一事物的计数不同?您可以发布表结构以进行生成查询。它们是从不同的表进行查询的吗?您在两个查询中都有相同的表名。在第一个查询中,我使用用户id,而在第二个查询中,我使用用户id来获取结果,这是不同的@巴尔马