Dan Vrajmas Dan Vrajmas - 2 months ago 10
PHP Question

Mysql Query with Cases

SELECT *, CASE
WHEN paydatefinal IS NULL OR paydatefinal = '0000-00-00' THEN paydate
ELSE paydatefinal END AS `paydatefinColumn`
FROM
`ws_crm_comenzi`
WHERE
`sezonId` = '4'
AND `service_id` = '1'
AND `finished` = '1'
AND `paydatefinColumn`='2016-09-26'
ORDER BY `paydatefinColumn` DESC


how can i make an AND From An Column (paydatefinColumn) that is made in a case but in table this column dose't exist

Answer

You need to repeat the CASE statement in the where clause

SELECT *, CASE
    WHEN paydatefinal IS NULL OR paydatefinal = '0000-00-00' THEN paydate
    ELSE    paydatefinal END AS `paydatefinColumn`
FROM
    `ws_crm_comenzi`
WHERE
    `sezonId` = '4'
AND `service_id` = '1'
AND `finished` = '1'
AND
 CASE
    WHEN paydatefinal IS NULL OR paydatefinal = '0000-00-00' THEN paydate
    ELSE    paydatefinal END='2016-09-26'
ORDER BY `paydatefinColumn` DESC