Created
June 18, 2011 16:21
-
-
Save nickdunn/1033239 to your computer and use it in GitHub Desktop.
wtf queries busting my balls
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
SELECT | |
`e`.id, `e`.section_id, e.`author_id`, UNIX_TIMESTAMP(e.`creation_date`) AS `creation_date` | |
FROM | |
`sym_entries` AS `e` | |
LEFT JOIN `sym_entries_data_10` AS `sbl` ON (`e`.`id` = `sbl`.`relation_id`) | |
LEFT JOIN `sym_member_replies` AS `replies` ON (`e`.`id` = `replies`.`entry_id` AND `replies`.`member_id` = 1) | |
WHERE | |
1 AND `e`.`section_id` = '1' | |
GROUP BY | |
`e`.`id` | |
HAVING | |
( | |
-- no entry_id in replies, member has never seen the thread | |
(SELECT `entry_id` FROM `sym_member_replies` WHERE `entry_id` = `e`.id LIMIT 1) IS NULL | |
-- latest child entry ID is larger than the last_seen | |
OR | |
( | |
( | |
SELECT MAX(`entry_id`) | |
FROM `sym_entries_data_10` | |
WHERE `sym_entries_data_10`.`relation_id` = `e`.`id` | |
GROUP BY `sym_entries_data_10`.`relation_id` | |
) | |
> (SELECT `last_read_entry_id` FROM `sym_member_replies` WHERE `entry_id` = `e`.id LIMIT 1) | |
) | |
) | |
ORDER BY | |
( | |
CASE | |
-- member has never seen the thread, it has replies | |
WHEN `replies`.`entry_id` IS NULL AND MAX(`sbl`.`entry_id`) IS NOT NULL THEN | |
MAX(`sbl`.`entry_id`) | |
-- member has never seen the thread, it has NO replies | |
WHEN `replies`.`entry_id` IS NULL AND MAX(`sbl`.`entry_id`) IS NULL THEN | |
`e`.`id` | |
-- member has read this thread | |
WHEN MAX(`sbl`.`entry_id`) > `replies`.`last_read_entry_id` THEN | |
MAX(`sbl`.`entry_id`) | |
ELSE | |
-1 | |
END | |
) DESC |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I think this makes me want to quit web development.