Mysql
 sql >> Datenbank >  >> RDS >> Mysql

MySQL-Datumsvergleichsfilter

WHERE date_column BETWEEN STR_TO_DATE('2010-12-10', '%Y-%m-%d') AND STR_TO_DATE('2010-12-15', '%Y-%m-%d')

So?

EDIT:Schlusszitat vergessen. Ups

EDITv2:Hinzufügen Ihres Codes mit der aktualisierten Abfrage

EDITv3:wenig Optimierung 1=1-Vergleich entfernt, verwendet IN() für wp_posts.post_status

SELECT      SQL_CALC_FOUND_ROWS wp_posts.* 
FROM        wp_posts 
  JOIN      wp_postmeta 
  ON        (wp_posts.ID = wp_postmeta.post_id) 
WHERE       wp_posts.post_type = 'post' 
  AND       (wp_posts.post_status IN ('publish','private'))
  AND       wp_postmeta.meta_key = 'expiry_date' 
  AND       ___INSERT_NAME_OF_DATE_COLUMN_HERE___
    BETWEEN STR_TO_DATE('2010-12-05', '%Y-%m-%d')
      AND   STR_TO_DATE('2010-12-15', '%Y-%m-%d')
GROUP BY    wp_posts.ID 
ORDER BY    wp_posts.post_date DESC 
LIMIT       0, 100