Invalid query SELECT post.* FROM ph JOIN post ON SUBSTRING_INDEX(ph.linked, ':', -1) = post.id AND post.pid = 1 WHERE ph.__tb = 'post:post:post' AND ph.seq < ORDER BY ph.seq DESC LIMIT 1: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'ORDER BY ph.seq DESC LIMIT 1' at line 1