我如何获得所有客户,但不能获得已在CURRENT月份注册的记录?我认为这会起作用,但不是 - 该怎么办?
SELECT * FROM customers WHERE deleted = 0 AND (MONTH(created) != MONTH(NOW() AND YEAR(created) != YEAR(NOW())))
答案 0 :(得分:3)
SELECT * FROM customers
WHERE (deleted = 0) AND not (
(MONTH(created) = MONTH(NOW()) AND
(YEAR(created) = YEAR(NOW()))
答案 1 :(得分:2)
作为一项基本规则,您应该避免使用列上的计算器,这会导致CPU使用率增加,并且无法使用索引。
标准的SQL兼容解决方案(也适用于MySQL)只是找到上个月的最后一天:
SELECT * FROM customers
WHERE deleted = 0
AND created <= CURRENT_DATE - EXTRACT(DAY FROM CURRENT_DATE)
答案 2 :(得分:0)
MONTH(NOW()
应该有一个结束括号,即: - MONTH(NOW())
MONTH(created) != MONTH(NOW())
将过滤掉创建日期月份不等于当月的客户
并YEAR(created) = YEAR(NOW())
将过滤当前年度
SELECT * FROM customers
WHERE deleted = 0
and MONTH(created) != MONTH(NOW())
and YEAR(created) = YEAR(NOW())
答案 3 :(得分:0)
试试这个
SELECT * FROM customers WHERE deleted = 0 AND not (
(MONTH(created) = MONTH(NOW()) AND
(YEAR(created) = YEAR(NOW()))
或强>
SELECT * FROM customers WHERE deleted = 0 AND !(
(MONTH(created) = MONTH(NOW()) AND
(YEAR(created) = YEAR(NOW()))
答案 4 :(得分:-1)
您应该将AND
更改为OR
:
select
*
from
customers
where
deleted = 0
and
(
year(created) != year(now()) or month(created) != month(now())
);