0

我有一个查询...

SELECT p.* 
FROM   wp_posts p 
       JOIN wp_postmeta pm 
         ON pm.post_id = p.ID 
       JOIN wp_postmeta pmd 
         ON pmd.post_id = p.ID 
WHERE  p.post_type = 'event' 
       AND p.post_status = 'publish' 
       AND ( pm.meta_key = 'post_city_id' 
             AND ( pm.meta_value = '1' ) ) 
       AND ( pmd.meta_key = 'end_date' 
             AND pmd.meta_value >= '2012-05-27' ) 
ORDER  BY p.post_date ASC, 
          p.post_title ASC 
LIMIT  5 

end_date 和 end_time 是元键,其值与特定帖子不应再显示在网站上的日期(以 Ymd 格式)和时间等价。所以我想用 pmd.meta_key='end_date' asc,pmd.meta_key='end_time' asc 之类的东西更改 p.post_date asc,但我不确定最好的方法。有什么想法吗?

4

1 回答 1

1

从问题中不能 100% 确定您要按什么进行排序,但如果您希望链接同一个表中的两个值(然后按以下方式排序),您可以按如下方式进行:

SELECT p.*
FROM wp_posts p  
  JOIN wp_postmeta pm ON pm.post_id=p.ID AND pm.meta_key='post_city_id' AND pm.meta_value='1' 
  JOIN wp_postmeta pmd ON pmd.post_id=p.ID AND pmd.meta_key='end_date' AND pmd.meta_value>='2012-05-27'
  JOIN wp_postmeta pmt ON pmt.post_id=p.ID AND pmt.meta_key='end_time'
WHERE p.post_type='event' and p.post_status='publish'  
ORDER BY pmd.meta_value, pmt.meta_value
LIMIT 5 

这将为您提供按 key = "end_date" 的 postmeta 值排序的结果,然后是 key = "end_time" 的 postmeta

(I've also moved the "WHERE" into the JOIN as this makes it clearer to read. Not required, but nicer for reading / debugging and is the same results.)

于 2012-05-28T04:06:54.253 回答