复杂的SQL查询辅助wordpress

时间:2013-04-08 11:05:46

标签: sql

我必须将其他两个表中的两个条件添加到此查询中,但我不知道该怎么做。

SELECT SQL_CALC_FOUND_ROWS * FROM wp_posts  WHERE 1=1  AND ( (
                SELECT COUNT(1)
                FROM wp_term_relationships
                WHERE term_taxonomy_id IN (2,3)
                AND object_id = wp_posts.ID
                ) = 2 ) AND wp_posts.post_type = 'packages' AND (wp_posts.post_status = 'publish' OR wp_posts.post_status = 'private') GROUP BY wp_posts.ID ORDER BY wp_posts.post_date DESC LIMIT 0, 10

其他表:

  

表1(name:packages_dates)有三个字段:   POST_ID,date_from(日期),DATE_TO(日期)

     

表2(name:packages_price)有两个字段:post_id,price(numeric)

条件是:

我有一个日期输入,必须在

中的date_from和date_to之间
  

表1

第二个输入是必须包含价格的价格范围

  

表2

感谢

1 个答案:

答案 0 :(得分:0)

如果您的数据库允许内部联接,那么在我看来这应该可行:

SELECT SQL_CALC_FOUND_ROWS * FROM wp_posts inner join packages_dates on wp_posts.ID=packages_dates.post_id inner join packages_price on wp_posts.ID=packages_price.post_id WHERE 1=1  AND ( (
            SELECT COUNT(1)
            FROM wp_term_relationships
            WHERE term_taxonomy_id IN (2,3)
            AND object_id = wp_posts.ID
            ) = 2 ) AND wp_posts.post_type = 'packages' AND (wp_posts.post_status = 'publish' OR wp_posts.post_status = 'private') AND packages_dates.date_from >=DateInput and packages_dates.date_to<=DateInput and packages_price between InputLowRange and InputHighRange GROUP BY wp_posts.ID ORDER BY wp_posts.post_date DESC LIMIT 0, 10