Answer the question
In order to leave comments, you need to log in
How to display NULL values for rows that do not match the conditions?
Good afternoon, I have a selection from the info table, the conditions table joins it with a LEFT JOIN, then in the WHERE section there is a condition that from conditions you need to select a row with the maximum value of the date_from field, provided that info.date_registration > conditions. date_from.
Those. the request looks something like this:
SELECT * FROM `info`
LEFT JOIN `conditions` ON `info`.`plan_id` = `conditions`.`plan_id`
WHERE `conditions`.`date_from` IN (
SELECT MAX(`c1`.`date_from`)
FROM `conditions` `c1`
WHERE `info`.`date_registration` > `c1`.`date_from`
)
Answer the question
In order to leave comments, you need to log in
So move the condition from "where" to "on" section of join
LEFT JOIN `conditions` ON `info`.`plan_id` = `conditions`.`plan_id` and
`conditions`.`date_from` IN (
SELECT MAX(`c1`.`date_from`)
FROM `conditions` `c1`
WHERE `info`.`date_registration` > `c1`.`date_from`
)
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question