Maybe this just isn’t possible. But the id
in table pin_board
is unknown inside of the subquery WHERE clause. If this isn’t possible, is there another way to do this in 1 sql statement?
SELECT
id
,name
,CASE WHEN is_private THEN 'b' ELSE 'a' END AS `orderBy`
,pin_board_image.image_path
FROM
pin_board
INNER JOIN (
SELECT
board_id
,image_path
FROM
pin
WHERE
board_id = pin_board.id
ORDER BY
date_created DESC
LIMIT 1
) AS pin_board_image ON pin_board.id = pin_board_image.board_id
WHERE
is_default = 1
OR (is_private = 1 && user_id = {$_SESSION['user_id']})
ORDER BY
`orderBy` ASC