Actually, your problem is that you have a list of values ββthat will be processed in MySQL as a string, and not as a set, in most cases. This is one of the possible solutions - correctly create your set in the application so that it looks like this:
SELECT '2013-02-20' UNION ALL SELECT '2013-06-30' UNION ALL SELECT '2013-12-19'
- and then use the resulting set inside the JOIN . Also, it will be great if MySQL can accept a static list in ANY subqueries - as for the IN keyword, but it cannot, ANY also expects a set of rows, not a list (which will be considered as a row with columns N , where N is number of items in your list).
Fortunately, in your particular case, your problem has an important limitation: the list cannot contain more elements than the rows in the foo table (otherwise it makes no sense). This way you can dynamically create this list and then use it like:
SELECT foo.*, final.period FROM (SELECT period, MAX(foo.effective_date) AS max_date FROM (SELECT period FROM (SELECT ELT(@i: =@i +1, '2013-02-20', '2013-06-30', '2013-12-19') AS period FROM foo CROSS JOIN (SELECT @i:=0) AS init) AS dates WHERE period IS NOT NULL) AS list LEFT JOIN foo ON foo.effective_date<list.period GROUP BY period) AS final LEFT JOIN foo ON final.max_date=foo.effective_date
- your list will be automatically repeated using ELT() , so you can pass it directly to the request without additional restructuring. Note that this method, however, will iterate over all foo entries to create a rowset, so it will work, but running the material in the application may be more useful in terms of performance.
A demo for your table can be found here .