我一直试图找出如何编写SQL查询以获取上周在系统中注册的用户数。这是我现在的问题:
SELECT count(id) FROM profile WHERE registration_date BETWEEN $week_ago AND $current_date
它呈现错误。使用的php变量在这里定义:
$week_ago = date('Y m d H:i:s', strtotime('-1 week'));
$current_date = date('Y m d H:i:s', time());
我做错了什么?
答案 0 :(得分:2)
您可以在纯SQL中执行此操作
SELECT count(id)
FROM profile
WHERE registration_date BETWEEN now() - interval 7 day
AND now()
答案 1 :(得分:1)
我不确定你的php,但你可以在sql中执行此操作:
SELECT count(id)
FROM profile
WHERE registration_date
BETWEEN NOW() - INTERVAL 7 DAY
AND NOW()
答案 2 :(得分:1)
如下所示
SELECT count(id)
FROM profile
WHERE registration_date BETWEEN (Cast(GetDate() as date)-7)
AND Cast(GetDate() as date)
或者你也试试这个
SELECT count(id)
FROM profile
WHERE registration_date >= (Cast(GetDate() as date)-7)
AND registration_date <= Cast(GetDate() as date)
答案 3 :(得分:0)
在SQL语句中使用$ week_ago和$ current_date周围的引号。
SELECT count(id) FROM profile WHERE registration_date BETWEEN '$week_ago' AND '$current_date'