Your query fails because you are doing a comparison and not in
:
SELECT *
FROM `epf_application_detail`
WHERE (application_id = 281656475 AND language_code = 'PL') OR
(language_code = 'EN' AND
application_id = 281656475 NOT IN (SELECT application_id
----------------------------------^
FROM `epf_application_detail` WHERE language_code = 'PL'))
MySQL is going to do the first comparison, and convert the boolean result to an integer for the not in
. Based on the structure of the first query, you want:
SELECT *
FROM `epf_application_detail`
WHERE (application_id = 281656475 AND language_code = 'PL') OR
(language_code = 'EN' AND
application_id NOT IN (SELECT application_id
FROM `epf_application_detail`
WHERE language_code = 'PL' and application_id is not null
)
)
I also added application_id is not null
, because NULL
can cause NOT IN
to fail.
EDIT:
Based on your comment, this should capture your logic:
SELECT *
FROM `epf_application_detail`
WHERE application_id = 281656475 AND
(language_code = 'PL' OR
(language_code = 'EN' AND
application_id NOT IN (SELECT application_id
FROM `epf_application_detail`
WHERE language_code = 'PL' and application_id is not null
)
)
)
If you are only looking for one row from the detail table, the following is much simpler:
SELECT *
FROM `epf_application_detail`
WHERE application_id = 281656475 AND
language_code in ('PL', 'EN')
ORDER BY language_code = 'PL' desc
LIMIT 1;
This uses MySQL-specific syntax. Your question is not tagged MySQL but does use MySQL syntax.