1

我有两个相同的表 - 登录和访问。其中包含:

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
4

2 回答 2

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
于 2013-09-20T13:44:39.147 回答
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

注意:我没有对此进行测试,因为我在这里没有您的架构/测试数据,但我认为这应该可行。如果没有,请告诉我。

于 2013-09-20T13:29:58.393 回答