按组排序不是wordpress sql

问题描述 投票:-2回答:1

似乎按组排序不适用于此。

变量$ check_all用于显示所有属性。

$check_all=$wpdb->get_results("select distinct town_village, suburb, state from $table where (concat(suburb,', ',state,', ',postcode)='$value' or concat(suburb,', ',postcode)='$value' or concat(suburb,', ',state,' ',postcode)='$value' or concat(suburb,', ',state)='$value' or CONCAT(town_village,', ',state) like '%$value%') Group by suburb,town_village,state", ARRAY_A);

然后我有这个代码通过搜索输入过滤输出。

$sql_keywords.= "properties.town_village in ('".$check_all[0]['town_village']."') AND properties.state in ('".$check_all[0]['state']."') AND ";

例如,结果必须像所有郊区'马尼拉'必须首先显示。请看截图http://prntscr.com/inpgjn

但我得到的结果是随机的。搜索郊区在结果中随机显示。

编辑:这是我上面显示的代码的当前结果

在'Manila'http://prntscr.com/inppj8搜索

我们在搜索“马尼拉”时想要的结果是首先显示所有马尼拉的结果http://prntscr.com/inpgjn

请参阅此功能的代码

function lookup_string($value, $sql_keywords, $sur_suburbs='0'){
        global $wpdb;
        $suburb_region=$wpdb->get_results("show columns from suburb_region");
        $table=($suburb_region)?'suburb_region':'properties';
        if($sur_suburbs==1){
            if(strpos($value,',')!==false){
                $check_all=$wpdb->get_results("select distinct town_village, suburb, state from $table where (concat(suburb,', ',state,', ',postcode)='$value' or concat(suburb,', ',postcode)='$value' or concat(suburb,', ',state,' ',postcode)='$value' or concat(suburb,', ',state)='$value' or CONCAT(town_village,', ',state) like '%$value%') Group by suburb,town_village,state", ARRAY_A);
                $check_suburb=$wpdb->get_var("select distinct suburb from $table where CONCAT(suburb,', ',state, ', ',postcode) like '%$value%'");
            }   
            if($check_all){

                $sql_keywords.= "properties.town_village in ('".$check_all[0]['town_village']."') AND properties.state in ('".$check_all[0]['state']."') AND ";
            }
        }
    }

预期的查询顺序如下所示。现在我随机进入郊区。


suburb | state |
----------------------
Manila | Metro |
Manila | Metro |
Manila | Metro |
Manila | Metro |
Pasay  | Metro |
Makati | Metro |
ETC    | Metro |
php mysql wordpress sorting
1个回答
2
投票

强制特定结果在排序顺序中排在第一位的一种方法是在CASE子句中使用ORDER BY语句,如下所示:

ORDER BY CASE `suburb`
    WHEN 'Manilla' THEN 'aaaaaaaa'
    ELSE `suburb` END, `state`

这是你的功能,里面有几个修改:

  1. 使用$ wpdb->准备使用preventing SQL injection attacks的“WordPress方式”。
  2. 给代码一些格式(间距)以帮助提高可读性。这是recommended by WordPress also
  3. 对SQL中的保留字(SELECT,FROM,OR,CONCAT等)使用UPPERCASE也有助于提高可读性。
  4. 在PHP中使用UPPERCASE为TRUE / FALSE等。
  5. 切换你的条件到Yoda style进行防御性编码。
  6. $wpdb->get_row()用于$check_all,因为你只关心第一个结果/行。

你的代码,修改过:

function lookup_string( $value, $sql_keywords, $sur_suburbs = 0 ) {
    global $wpdb;
    $suburb_region = $wpdb->get_results( "SHOW COLUMNS FROM suburb_region" );
    $table=( $suburb_region ) ? 'suburb_region' : 'properties';

    if( (int)$sur_suburbs ) {
        // Your use of $check_all below will throw notices without this
        $check_all = FALSE;
        if( FALSE !== strpos( $value, ',' ) ) {
            // this is how you format "LIKE" queries using $wpdb->prepare
            $like = "%{$value}%";

            // To prevent SQL injection, use $wpdb->prepare
            $prepared = $wpdb->prepare( "SELECT distinct town_village, suburb, state 
                FROM {$table} 
                    WHERE (CONCAT(suburb, ', ', state, ', ', postcode)=%s 
                    OR CONCAT(suburb, ', ', postcode)=%s 
                    OR CONCAT(suburb, ', ', state,' ', postcode)=%s 
                    OR CONCAT(suburb, ', ', state)=%s 
                    OR CONCAT(town_village, ', ' ,state) LIKE %s) 
                    GROUP BY suburb, town_village, state 
                    ORDER BY CASE `suburb`
                        WHEN 'Manilla' THEN 'aaaaaaaa'
                        ELSE `suburb` END, state",
            $value, $value, $value, $value, $like);
            // execute the prepared query
            // Use get_row to get a single row
            $check_all = $wpdb->get_row( $prepared );

            // To prevent SQL injection, use $wpdb->prepare
            $prepared = $wpdb->prepare( "SELECT DISTINCT suburb FROM $table WHERE CONCAT(suburb, ', ', state, ', ', postcode) LIKE %s", $like );
            // execute the prepared query
            $check_suburb = $wpdb->get_var( $prepared );
        }  

        if( $check_all ) {
            // modified $check_all to use object notation, since using get_row above
            $sql_keywords.= "properties.town_village IN ('". $check_all->town_village ."') AND properties.state IN ('" . $check_all->state . "') AND ";
        }
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.