inner join on mysql just work with one column

I do not know if I awake stupid but I can not solve this :S

I am triying to get items form wp_posts table, joining it with the table wp_postmeta two times (to get to diferent values) and making conditions with this two values.

Read More
SELECT SQL_CALC_FOUND_ROWS DISTINCT wp_posts.* FROM wp_posts
INNER JOIN wp_postmeta ds ON (ds.meta_key = 'date_start' AND ds.post_id = wp_posts.ID)
INNER JOIN wp_postmeta de ON (de.meta_key = 'date_end' AND ds.post_id = wp_posts.ID)
WHERE 1 = 1
  AND wp_posts.post_type = 'event'
  AND (wp_posts.post_status = 'publish' OR wp_posts.post_status = 'closed' OR wp_posts.post_status = 'private' OR wp_posts.post_status = 'hidden')
  AND CAST(ds.meta_value AS DECIMAL(10,0)) <= 1323043200
  AND (CAST(de.meta_value AS DECIMAL(10,0)) >= 1323129599 OR de.meta_value = '')
ORDER BY ds.meta_value DESC
LIMIT 0, 5

I do not why all columns join with only one row in the 'de' table

I mean:

ID meta_1 post_id meta_2
1     1       1    xxx
2     2       1    xxx
3     3       1    xxx
...

In the condition de.meta_key = 'date_end' AND ds.post_id = wp_posts.ID I am telling the the ID from wp_posts should be equal to post_id from 'de' table (wp_postmeta) but it is not been respected 😐

Any idea of my mistake???

Related posts

Leave a Reply

1 comment

  1. Change

    (de.meta_key = 'date_end' AND ds.post_id = wp_posts.ID)
    --> repeated match of ds.post_id=wp.posts.ID
    

    To

    (de.meta_key = 'date_end' AND de.post_id = wp_posts.ID)