【问题标题】:Remove duplicated towns in select menu在选择菜单中删除重复的城镇
【发布时间】:2017-06-19 13:11:46
【问题描述】:

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

select ID from $wpdb->posts where post_type = 'place' and post_parent = $cities order by post_title asc 

问题在于,有些城镇会多次显示,例如“Berlin Berlin Berlin”。如何使用 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;
    }

【问题讨论】:

  • 它们在数据库中是否重复?
  • 是的,因为我想通过查询得到它们
  • 将查询的开头更改为 - SELECT DISTINCT ID
  • 使用select disitnctGroup by
  • 它不工作

标签: php mysql sql wordpress


【解决方案1】:

试试:

select DISTINCT(ID) from $wpdb->posts where post_type = 'place' and post_parent = $cities order by post_title asc

DISTINCT 将从表中返回唯一的ID's

【讨论】:

  • 还是同样的问题
【解决方案2】:

如果变量不在array() 中,请先将它们放入数组中。并使用array_unique($town);($town 基于您的数组变量)

array_unique() 将确保您在数组中的项目将返回一个没有重复值的新数组。

更多详情参考:http://php.net/manual/en/function.array-unique.php

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-02-03
    • 1970-01-01
    • 2017-05-03
    • 1970-01-01
    • 1970-01-01
    • 2015-04-04
    • 2011-01-22
    • 1970-01-01
    相关资源
    最近更新 更多