Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/285.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 在“选择”菜单中删除重复的城镇_Php_Mysql_Sql_Wordpress - Fatal编程技术网

Php 在“选择”菜单中删除重复的城镇

Php 在“选择”菜单中删除重复的城镇,php,mysql,sql,wordpress,Php,Mysql,Sql,Wordpress,我有一个选择菜单选项,可以通过以下方式获取城市: select ID from $wpdb->posts where post_type = 'place' and post_parent = $cities order by post_title asc 问题是有些城镇不止一次被展示,比如“柏林”。如何使用sql查询或其他方法删除重复的城镇 以下是整个文件: public static function get_destination_countries() { gl

我有一个选择菜单选项,可以通过以下方式获取城市:

select ID from $wpdb->posts where post_type = 'place' and post_parent = $cities order by post_title asc 
问题是有些城镇不止一次被展示,比如“柏林”。如何使用sql查询或其他方法删除重复的城镇

以下是整个文件:

public static function get_destination_countries() {
        global $wpdb;

        $countries = $wpdb->get_results( "select ID, post_title from $wpdb->posts where post_type = 'place' and post_parent = 0 order by post_title asc ", ARRAY_A );

        return $countries;
    }

    /**
     * Fetch all destination states by a country
     *
     * @return array all states by a country, array with post_ID and post_title
     */
    public static function get_destination_states( $country_id ) {
        global $wpdb;
        if(!empty($country_id)){
            // We need to make sure the destination state has ACF "state"
            $place_type_tax_id = '';
            $place_type_tax = get_term_by( 'slug', 'state', 'place-type' );
            if ( ! empty( $place_type_tax ) && ! is_wp_error( $place_type_tax ) ) {
                $place_type_tax_id = $place_type_tax->term_id;
            }

            //$states = $wpdb->get_results( "select ID, post_title from $wpdb->posts where post_type = 'place' and post_parent = $country_id order by post_title asc", ARRAY_A );
            $states = $wpdb->get_results( "select p.ID  , p.post_title, pm.meta_value from $wpdb->postmeta pm inner join $wpdb->posts p where p.post_parent = $country_id and p.ID = pm.post_id and meta_value = '$place_type_tax_id' order by post_title asc" , ARRAY_A );

            return $states;
            }
        return false;
    }

    /**
     * Get a list of all cities in a State
     */
    public static function get_cities_from_state( $state_id ) {
        global $wpdb;
        if(!empty($state_id)){
            $cities = $wpdb->get_col( "select ID from $wpdb->posts where post_type = 'place' and post_parent = $state_id order by post_title asc" );
            return $cities;
        }

        return false;
    }

    /**
     * Pull up a country ID by name - helpful to get United States as an ID in any DB
     */
    public static function get_destination_country_id_by_name( $country_name ) {
        global $wpdb;
        if(!empty($country_name)){
            $country_id = $wpdb->get_var( "select ID from $wpdb->posts where post_type = 'place' and post_title = '$country_name' order by post_title asc " );
            return $country_id;
        }

        return false;
    }

    /**
     * Pull destination's (countries, states etc) name by place ID
     */
    public static function get_destination_name_by_id( $place_id ) {
        global $wpdb;
        if(!empty($place_id)){
            $destination_name = $wpdb->get_var( "select post_title from $wpdb->posts where post_type = 'place' and ID = $place_id order by post_title asc" );
            return $destination_name;
        }

        return false;
    }

    /**
     * Fetch the country ID by state ID being a parent - used for the results page
     */
    public static function get_country_by_state_id( $place_id ) {
        global $wpdb;
        if(!empty($place_id)){
            $parent_id = $wpdb->get_var( "select post_parent from $wpdb->posts where post_type = 'place' and ID = $place_id order by post_title asc" );
            return $parent_id;
        }

        return false;
    }

    /**
     * Needed for the destination results page
     *
     * Fetch all destinations attached through ACF via state
     */
    public static function get_destination_ids_by_state( $state_id ) {

    }

    /**
     * Get destinations in a list of cities, TBD
     */
    public static function get_destinations_in_cities( $cities ) {
        global $wpdb;
        if(!empty($cities)){
            $cities = $wpdb->get_col( "select ID from $wpdb->posts where post_type = 'place' and post_parent = $cities order by post_title asc" );
            return $cities;
        }
        return false;
    }
尝试:


DISTINCT
将从表中返回唯一的
ID

如果变量不在
array()
中,则将它们放在数组的第一位。并使用
array\u unique($town)($town基于数组变量)

array\u unique()
将确保数组中的项将返回一个新数组,而不返回重复值


有关更多详细信息,请参考:

它们是否在数据库中重复?是的,因为我是通过查询获得它们的,所以我想将查询的开始更改为-SELECT DISTINCT IDuse
SELECT disitnct
groupby
这仍然是一个问题
select DISTINCT(ID) from $wpdb->posts where post_type = 'place' and post_parent = $cities order by post_title asc