MySQL: select closest date before specific date
<p>How to select only closest date (select full row) before specific date for each <code>user_id</code>? (select single row for each user)</p>

<p>table sample:</p>

<pre><code>id     user_id     create_date
0         1         2016-01-01
1         1         2016-01-05
2         2         2016-01-06
3         3         2016-01-06
4         2         2016-01-09
5         2         2016-01-12
6         4         2016-01-15
7         1         2016-01-21
8         2         2016-01-22
9         5         2016-01-25
</code></pre>

<p>SQL sample: (seems not working as expected)</p>

<pre><code>select t.id,t.user_id,MAX(t.create_date)
from sample_table t
where date(t.create_date) &lt; '2016-01-20'
group by t.user_id
limit 10
</code></pre>

<p>expected result:</p>

<pre><code>id     user_id     create_date
1         1         2016-01-05
3         3         2016-01-06
5         2         2016-01-12
6         4         2016-01-15
</code></pre>