SELECT distinct a.* ,b.* ,case when c. FK_voucher_map_id = 1122 then 1 else 0 end as voucher_flag FROM `tbl_picture_type_map` a inner join `tbl_hotel_gallery` b on a.FK_pic_id = b.pic_id left join tbl_gallery_voucher_map c on b.pic_id = c.FK_pic_id where `FK_picture_type_id` = 4 and FK_hotel_id = and FK_voucher_map_id = 1122 and c.FK_pic_id is not null ---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 'and FK_voucher_map_id = 1122 and c.FK_pic_id is not null' at line 12