我有两个相同的表 - 登录和访问。其中包含:
ID | address | mac_address | date
如何制作一个php查询来计算来自mac地址的登录和访问类型:
MAC | Logins | Visits
现在我有
SELECT mac_address, COUNT(mac_address) AS login_cnt FROM logins GROUP BY mac_address ORDER BY login_cnt DESC
给了我
MAC | Logins
答案 0 :(得分:2)
SELECT
mac_address,
SUM(logins_mac_count) AS logins_mac_sum,
SUM(visits_mac_count) AS visits_mac_sum
FROM (
SELECT mac_address, COUNT(*) AS logins_mac_count, 0 AS visits_mac_count
FROM logins
GROUP BY mac_address
UNION
SELECT mac_address, 0 AS logins_mac_count, COUNT(*) AS visits_mac_count
FROM visits
GROUP BY mac_address
) AS t
GROUP BY mac_address
答案 1 :(得分:0)
试试这个:
SELECT
IFNULL(logins.mac_address, visits.mac_address) AS unique_mac_address,
COUNT(login.mac_address) AS count_logins,
COUNT(visits.mac_address) AS count_visits
FROM logins
LEFT JOIN visits ON logins.mac_address = visits.mac_address
GROUP BY unique_mac_address
注意:我没有对此进行测试,因为我这里没有您的架构/测试数据,但我认为这应该有效。如果没有,请告诉我。