M
M
maks2807952016-02-08 11:32:29
MySQL
maks280795, 2016-02-08 11:32:29

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`
)

The question is, how do I output all rows from info by substituting NULL values ​​when the date_registration > conditions.date_from condition is false?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
P
Peter, 2016-02-08
@maks280795

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 question

Ask a Question

731 491 924 answers to any question