0

我有很多自定义字段 - 价格、重量等............

现在我正在尝试基于此创建自定义搜索,并且我想按最小值和最大值进行搜索......不知何故我无法创建确切的查询............将感谢某人对此的帮助..

这是我正在尝试的查询

$where = "";
if ( isset( $_POST['pricemin']) && $_POST['pricemin'] != "" ) {
    $pricemin = $_POST['pricemin'];
    $where .= " and (SELECT post_id from wp_postmeta where  meta_key='list_price' and meta_value > ".$pricemin.$order.") ";
}
if ( isset( $_POST['pricemax']) && $_POST['pricemax'] != "" ) {
    $pricemax = $_POST['pricemax'];
    $where .= " and (SELECT post_id from wp_postmeta where  meta_key='list_price' and meta_value < ".$pricemax.$order.") ";
}

这是最后一行..

"SELECT * from wp_posts where ID IN (select 1 from wp_postmeta ".$where.")"

关于 UNION、INTERSECT 等的任何建议或最好的方法............谢谢

其实 mysql Between 解决了我一半的问题......我可以全部写成

if ( isset( $_POST['pricemin'] ) && $_POST['pricemin'] != "" ) {
    $pricemin = $_POST['pricemin'];
    $wherep1= "  (SELECT post_id from wp_postmeta where  meta_key='list_price' and meta_value > ".$pricemin.$order.") ";
}
if ( isset( $_POST['pricemax'] ) && $_POST['pricemax'] != "" ) {
    $pricemax = $_POST['pricemax'];
    $wherep1 = " (SELECT post_id from wp_postmeta where  meta_key='list_price' and meta_value < ".$pricemax.$order.") ";
}
if ( isset($_POST['pricemin'] ) && $_POST['pricemin'] != "" && isset( $_POST['pricemax'] ) && $_POST['pricemax'] != "" ) {
    $pricemin = $_POST['pricemin'];
    $pricemax = $_POST['pricemax'];
    $wherep1 = " (SELECT post_id from wp_postmeta where  meta_key='list_price' and meta_value BETWEEN ".$pricemin." and ".$pricemax ." ".$order.") ";
}

但是后来我也想搜索体重,身高............

所以,像 Intersect 这样的东西可以帮助我..但我看到 MYSQL 不支持 INTERSECT........所以,问题仍然是如何重写查询

A 相交 B 相交 C 相交 D 在 mysql 中

4

2 回答 2

0

将您的最终查询修改为:

"SELECT * from wp_posts where ID IN (select post_id from wp_postmeta ".$where.")"
于 2013-09-19T02:25:46.547 回答
-1

得到了答案......

SELECT * from wp_posts where ID IN ( (SELECT post_id from wp_postmeta where meta_key='list_price' and meta_value BETWEEN 1 and 1229000 ) ) and id in (SELECT post_id from wp_postmeta where meta_key='weight' and meta_value BETWEEN 20 and 200 ) 

使用Between & And 子句!

于 2013-09-19T02:50:09.333 回答