Reputation: 13
$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'
AND lastactivity2 < UNIX_TIMESTAMP(DATE_SUB(now(), INTERVAL 3 YEAR))
ORDER BY paid DESC, lastactivity2 DESC, id1 ASC
LIMIT 100
";
Upvotes: 0
Views: 43
Reputation: 13
The solution was that I need to change the LEFT JOIN on the user table to an INNER JOIN since the lastactivity column, which I am adding to there WHERE clause, is in that table. (I also need to change it to "lastactivity" instead of "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
";
Upvotes: 0
Reputation: 704
if you are trying to get entries that were active within the last 3 years, your comparison should be a greater than:
lastactivity2 > UNIX_TIMESTAMP(DATE_SUB(now(), INTERVAL 3 YEAR))
Time moves forward, newer times are a bigger number than older times.
What you probably thought was that you wanted to compare 3 years to the time interval between today and the last activity, but you are comparing absolute timestamps, not differences.
Upvotes: 1