Wordpress自定义字段的高级Mysql

时间:2013-09-19 02:13:59

标签: sql wordpress

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

现在我正在尝试基于此创建自定义搜索,并且我希望按最小值和最小值搜索最大...不知怎的,我无法创建确切的查询.........会感谢别人的帮助...

这是我正在尝试的查询

$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之间解决了我的一半问题......我可以把它全部写成

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 ........所以,问题仍然是如何重写查询

在mysql中的INTERSECT B INTERSECT C INTERSECT D

2 个答案:

答案 0 :(得分:0)

将您的最终查询修改为:

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

答案 1 :(得分:-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&amp;和条款!