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 `produto`.excluido IN ('0','2') ORDER BY RAND() LIMIT 4' at line 1 - SELECT id, titulo, valor, (SELECT nome_foto FROM produto_img WHERE produto_fk = produto.id ORDER BY ordem ASC, data_cadastro DESC, id DESC LIMIT 1) as nome_foto FROM `produto` WHERE id <> 3 AND produto_ctg_fk = AND `produto`.excluido IN ('0','2') ORDER BY RAND() LIMIT 4