1064: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'AND wall_item.item_id != this_item.item_id GROUP BY wall_item.ite...' at line 14 SELECT wall_item.item_id, wall_item.title, wall_item.client, wall_item.sidebar_image, wall_item.slug, YEAR(wall_item.date) AS date, COUNT(DISTINCT similar.tag_id) as shared_tags FROM wall_item INNER JOIN ( wall_item_has_tag AS this_item INNER JOIN wall_item_has_tag AS similar USING (tag_id) ) ON similar.item_id = wall_item.item_id WHERE this_item.item_id = AND wall_item.item_id != this_item.item_id GROUP BY wall_item.item_id ORDER BY shared_tags DESC LIMIT 7