我想只选择今天的日期记录?我正在使用此查询:
$query = mysql_query (
"SELECT
tentego_img.*
FROM
tentego_img
INNER JOIN tentego_img_vote ON tentego_img.id = tentego_img_vote.object_id
WHERE
cat = '".$cat [ ' id ' ]."'
GROUP BY
tentego_img_vote.object_id
ORDER BY
SUM(
(
CASE
WHEN tentego_img_vote.vote = '0'
AND DATE(date) = DATE(NOW()) THEN
'1'
ELSE
'-1'
END
)
) DESC
LIMIT 1"
);
在此摘录代码中:
(CASE WHEN tentego_img_vote.vote = '0' AND DATE(date) = DATE(NOW()) THEN '1' ELSE '-1' END)
如何在今天的日期选择?
答案 0 :(得分:3)
这是您的查询,已修复:
GROUP BY
字段。以下是查询:
SELECT i.*
FROM tentego_img i INNER JOIN
tentego_img_vote iv
ON i.id = iv.object_id
WHERE cat = '".$cat['id']."'
GROUP BY i.id
ORDER BY SUM(CASE WHEN iv.vote = 0 AND DATE(date) = DATE(NOW())
THEN 1 ELSE -1 END) DESC
LIMIT 1;
如果您只想从今天开始投票,请将条件移至WHERE
子句:
SELECT i.*
FROM tentego_img i INNER JOIN
tentego_img_vote iv
ON i.id = iv.object_id
WHERE cat = '".$cat['id']."' AND
DATE(date) = CURDATE()
GROUP BY i.id
ORDER BY SUM(CASE WHEN iv.vote = 0
THEN 1 ELSE -1 END) DESC
LIMIT 1;
更好的方法是使用不等式而不是函数:
WHERE cat = '".$cat['id']."' AND
(date >= CURDATE() AND
date < DATEADD(CURDATE(), interval 1 day)
)
这样做更好,因为如果合适,它可以在date
上使用索引。
答案 1 :(得分:0)
这应该适合您,更改dateformat以匹配您在数据库中使用的格式:
(CASE WHEN tentego_img_vote.vote = '0' AND STR_TO_DATE(date, '%d.%m.%Y') > CURRENT_TIMESTAMP - INTERVAL 1 DAY) THEN '1' ELSE '-1' END) //where 'date' is the column name of the date you're checking.