MySQL:如何以这种方式获得不同的数字?

时间:2012-05-02 02:22:04

标签: mysql

在我的MySQL数据库中,我有一个表(例如T),其中包含mac地址和时间(例如,2012-4-30 23:00:00)。我想过滤掉本周出现的那些mac地址,但不是在前一周。

我怎么能用一句话来做?我尝试使用多个选择,但还没有找到方法。

提前致谢。

3 个答案:

答案 0 :(得分:0)

您可以使用YEARWEEK

要获取本周的记录:

SELECT * FROM T WHERE YEARWEEK(your_datetime_column) = YEARWEEK(NOW());

仅获取前几周的记录:

SELECT * FROM T WHERE YEARWEEK(your_datetime_column) < YEARWEEK(NOW());

答案 1 :(得分:0)

如果没有表结构,我建议像:

SELECT [DISTINCT] Mac FROM table t
WHERE 
t.Time BETWEEN '2012-01-09 00:00:00' AND '2012-01-16 23:59:59'
AND 
NOT EXISTS (SELECT * FROM FROM table t2 WHERE t2.Mac = t.Mac AND T2.Time BETWEEN '2012-01-01 00:00:00' AND '2012-01-08 23:59:59'

答案 2 :(得分:0)

要获得本周有时间但前一周没有时间的Mac地址,可以使用以下内容:

SELECT *
FROM T
WHERE YEARWEEK(T.Time) = YEARWEEK(NOW())
AND T.MacAddress NOT IN (
    SELECT MacAddress
    FROM T AS T2
    WHERE YEARWEEK(T2.Time) = YEARWEEK(TIMESTAMPADD(WEEK, -1, NOW())));

这使用bfavaretto已经建议的YEARWEEK函数,以及计算前一周的TIMESTAMPADD函数。