如何将两个表与“count”组合在一起

时间:2016-04-05 03:47:38

标签: mysql sql combinations

需要你的帮助,我如何加入以下两张桌子?他们每个人都有“计数”功能。

表1:

SELECT page,   count(page)
FROM table 1
WHERE date = '2016-04-04'
GROUP BY page

输出(表1):

page | count(page) |
231  |           1 |
232  |           2 |
233  |           3 |

表2:

SELECT page,   count(page)
FROM table 2
WHERE date = '2016-04-04'
GROUP BY page

输出(表2):

page | count(page) |
231  |           4 |
232  |           5 |
233  |           6 |

我想要实现的是这样的:

page | count(page) | page1  |  count(page1)  |
231  |           1 |     231|             4  |
232  |           2 |     232|             5  |
233  |           3 |     233|             6  |

3 个答案:

答案 0 :(得分:0)

如果你能做点什么

SELECT COUNT(*) as "Files" FROM files WHERE Folder = n;

它被称为相关子查询。

http://dev.mysql.com/doc/refman/5.1/en/correlated-subqueries.html 1

答案 1 :(得分:0)

这两个结果表之间的INNER JOIN将完成工作。

{{1}}

答案 2 :(得分:0)

SELECT t1.page AS 'page',
       t1.count AS 'count(page)',
       t2.page AS 'page1',
       t2.count AS 'count(page1)'
FROM table_1 AS t1,
     table_2 AS t2
WHERE t1.page=t2.page