Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我有 2 个不同的表,其中 user_id 作为公共字段。
如何编写查询来计算两个表中每个用户的总出现次数。
比如说。两个表中的用户分别有 2 和 3 条记录。我需要用户 ID 和总记录(即)5
SELECT COUNT(*) FROM ( SELECT userid FROM table1 UNION ALL SELECT userid FROM table2 ) WHERE userid = '123'
对于所有用户*s*,
SELECT userid, COUNT(*) FROM ( SELECT userid FROM table1 UNION ALL SELECT userid FROM table2 ) t GROUP BY userid