Reputation: 4196
I have a meta value which is being joined onto my posts which is the date. I want to be able to only get future posts but this date is a varchar and is in the format of
dd-mm-yyyy
This is what I've tried so far but doesn't seem to work properly
select `posts`.*, `storage_varchars`.`meta_value`
from `posts` left join `storage_varchars` on
`posts`.`id` = `storage_varchars`.`post_id`
where `parent_id` = 20 and
DATE(storage_varchars.meta_value) >= NOW()
order by DATE(storage_varchars.meta_value) asc
Upvotes: 3
Views: 6734
Reputation: 29051
Use STR_TO_DATE() function:
Try this:
SELECT p.*, s.meta_value
FROM posts p
LEFT JOIN storage_varchars s ON p.id = s.post_id
WHERE parent_id = 20 AND STR_TO_DATE(s.meta_value, '%d-%m-%Y') >= CURRENT_DATE()
ORDER BY STR_TO_DATE(s.meta_value, '%d-%m-%Y') ASC;
Upvotes: 10
Reputation: 28403
try this
select `posts`.*, `storage_varchars`.`meta_value`
from `posts` left join `storage_varchars` on
`posts`.`id` = `storage_varchars`.`post_id`
where `parent_id` = 20 and
STR_TO_DATE(storage_varchars.meta_value, '%d %M %Y') >= NOW()
order by STR_TO_DATE(storage_varchars.meta_value, '%d %M %Y') DESC
Upvotes: 0