$query = "SELECT user.userid AS id3, user.lastactivity AS lastactivity1, DATE_FORMAT(FROM_UNIXTIME(user.lastactivity), '%Y %m %d') AS 'lastactivity2', socialgroup.groupid AS id1, socialgroupicon.groupid AS id2, socialgroupicon.dateline AS picdate, name, zip1, city1, state1, socialgroup.dateline AS groupdate, creatoruserid, members, picturecount, visible, paid
FROM socialgroup
LEFT JOIN socialgroupicon
ON socialgroupicon.groupid=socialgroup.groupid
LEFT JOIN user
ON user.userid=socialgroup.creatoruserid
WHERE type != 'inviteonly' AND state1 = '$region'
和lastactivity2< UNIX_TIMESTAMP(DATE_SUB(now(),INTERVAL 3年))
ORDER BY paid DESC, lastactivity2 DESC, id1 ASC
LIMIT 100
";
答案 0 :(得分:1)
如果您尝试获取过去3年内有效的条目,则您的比较应大于:
lastactivity2 > UNIX_TIMESTAMP(DATE_SUB(now(), INTERVAL 3 YEAR))
时间向前发展,新时代比旧时代更大。
您可能认为您希望将3年与今天和最后一项活动之间的时间间隔进行比较,但您要比较绝对时间戳,而不是差异。
答案 1 :(得分:0)
解决方案是我需要将用户表上的LEFT JOIN更改为INNER JOIN,因为我添加到WHERE子句的lastactivity列位于该表中。 (我还需要将其更改为" lastactivity"而不是" lastactivity2)。
$query = "SELECT user.userid AS id3, user.lastactivity AS lastactivity1, DATE_FORMAT(FROM_UNIXTIME(user.lastactivity), '%Y %m %d') AS 'lastactivity2', socialgroup.groupid AS id1, socialgroupicon.groupid AS id2, socialgroupicon.dateline AS picdate, name, zip1, city1, state1, socialgroup.dateline AS groupdate, creatoruserid, members, picturecount, visible, paid
FROM socialgroup
INNER JOIN user
ON user.userid=socialgroup.creatoruserid
LEFT JOIN socialgroupicon
ON socialgroupicon.groupid=socialgroup.groupid
WHERE type != 'inviteonly' AND state1 = '$region' AND lastactivity > UNIX_TIMESTAMP(DATE_SUB(now(), INTERVAL 3 YEAR))
ORDER BY paid DESC, lastactivity2 DESC, id1 ASC
LIMIT 100
";