Search code examples
mysqlwordpresssql-like

MYSQL - Multiple Select and LIKE Method


What i want is: Get posts with date greater then 2010-03-02 and with the meta_value 'something' + like '2010-'

because there are other values like 239048192304 or 293811743

$query = "SELECT DISTINCT wp_postmeta.meta_key, wp_postmeta.meta_value, wp_posts.ID, wp_posts.guid, wp_postmeta.post_id, wp_posts.post_title
FROM wp_postmeta
INNER JOIN wp_posts
ON wp_postmeta.post_id=wp_posts.ID
WHERE wp_postmeta.meta_value >='2010-03-02'
AND wp_postmeta.meta_value = 'something'
AND wp_postmeta.meta_value LIKE '2010-'
ORDER BY wp_postmeta.meta_value ASC
LIMIT 0,10";

can you help me out please? thank you!

Update2:

table wp_postmeta

post_id | meta_value    
5       | 2010-12-30    
5       | Berlin

3       | 2010-12-29
3       | Paris

2       | 2009-12-29
2       | Paris

14      | 12232456521
14      | Berlin

Output:

2010-12-30 Berlin ID 5
2010-12-29 Paris  ID 3

Solution

  • A larger data set and sample answer would help clarify the question but here is my interpretation of what you are looking for. It's not elegant but if you've got the buffer space allocated it works.

    SELECT DISTINCT wp_postmeta.meta_key, wp_postmeta.meta_value, wp_posts.ID, wp_posts.guid, wp_postmeta.post_id, wp_posts.post_title
    FROM wp_postmeta
    INNER JOIN wp_posts
    ON wp_postmeta.post_id=wp_posts.ID
    WHERE wp_postmeta.post_id IN (
        select post_id from wp_postmeta where str_to_date(meta_value, '%Y-%m-%d') >= 2010-03-02' and post_id in (select post_id from wp_postmeta where meta_value = 'something')
    );