0

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

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),date_to(date)

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

条件是:

我有一个日期输入,它必须在 date_from 和 date_to 之间

表格1

;

第二个输入是一个价格范围,必须包括价格

表 2

;

谢谢

4

1 回答 1

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
于 2013-04-08T20:55:00.687 回答