Помогите составить правильно запрос
в phpmyadmin проверял, выполняется, но ругается на CASE WHEN (неизвестное ключевое слово)
$query = $this->db->query("SELECT c.customer_id, c.firstname, c.lastname, cc.chat_id, cc.sender, cc.unread, cm.message, cm.date_added
FROM " . DB_PREFIX . "customer c, " . DB_PREFIX . "customer_chat cc
LEFT JOIN " . DB_PREFIX . "customer_messages cm ON (cc.last_message_id = cm.message_id)
WHERE (cc.first = '" . (int)$customer_id . "' OR cc.second` = '" . (int)$customer_id . "')
AND
CASE
WHEN cc.first = '" . (int)$customer_id . "' THEN cc.second = c.customer_id AND cc.first_delete = '0'
WHEN cc.second` = '" . (int)$customer_id . "' THEN cc.first` = c.customer_id` AND cc.second_delete = '0'
END
ORDER BY cc.unread DESC");
Выполняю запрос через контролер:
Fatal error: Uncaught Exception: Error: 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 '` = '1') AND CASE WHEN cc.first = '1' THEN cc.second = c.customer_id AND cc.firs' at line 1<br />Error No: 1064