我有 2 张桌子。
表交易:
id | customer_id | department_id
--------------------------------
1 | 1 | 2
--------------------------------
2 | 2 | 3
--------------------------------
3 | 2 | 4
--------------------------------
4 | 3 | 1
--------------------------------
5 | 2 | 3
--------------------------------
表des_department
id | caption
-----------------
1 | department1
-----------------
2 | department2
-----------------
3 | department3
-----------------
4 | department4
-----------------
5 | department5
-----------------
我需要为每个customer_id显示部门标题。选择用户访问次数最多的部门。
输出示例:
customer_id | caption
------------------------------
1 | department2
------------------------------
2 | department3
------------------------------
3 | department1
------------------------------
我也有自己的查询。但我显示所有访问次数。
我的查询:
SELECT t.customer_id, t.terminal_i
FROM transaction t WHERE (t.customer_id, t.terminal_id) IN
( SELECT t1.customer_id, t1.terminal_id
FROM transaction t1 GROUP BY t1.customer_id
)
我在 dbForge for Mysql 上工作。