【发布时间】: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 disitnct或Group by -
它不工作