Home > Software engineering >  How to create a LIKE Between query?
How to create a LIKE Between query?

Time:04-06

So I have issue with searching through wp_comments table. This is some custom meta created by the custom plugin. This is what needs to be searched for https://prnt.sc/a2IXbLNVKs11

$q = "SELECT comment_id, meta_value 
        FROM wp_commentmeta 
        WHERE meta_key = 'course_completion' 
        AND meta_value LIKE '" . $course\['id'\]. "%," . date('Y-m-d', strtotime('-24 months')) . "%'";

Except that I need to implement in it Between two dates.

For example:

Between date('Y-m-d', strtotime('-24 months')) 
    and date('Y-m-d', strtotime('-2 months'))

I have tried subquery, nested query, and everything I could think about, but no luck, no results for searching between two dates.

Can you help? Thank you

CodePudding user response:

A little look through the MySQL manual for String Functions that might help me and refresh my memory on the str_to_date() function and the use of MySQL's own date manipulations using CURDATE() and the INTERVAL and Bob's your Uncle.

$q = "SELECT comment_id, meta_value 
      FROM wp_commentmeta 
      WHERE meta_key = 'course_completion' 
      AND str_to_date( substr(meta_value, locate(',', meta_value) 1, 10), '%Y-%m-%d') 
            BETWEEN CURDATE() - INTERVAL -24 month 
            AND CURDATE() - INTERVAL -2 month";
  • Related