1

wordpress データベースでの mysql クエリに問題があります。

国と都市のフィールドをカスタムしました。

現時点では、「UK」国のすべての都市を表示するこのクエリがあります

$querystr = "
SELECT wposts.* 
FROM $wpdb->posts wposts, $wpdb->postmeta wpostmeta
WHERE wposts.ID = wpostmeta.post_id 
AND wpostmeta.meta_key  = 'Country' 
AND wpostmeta.meta_value  = 'UK' 
AND wposts.post_type = 'post' 
ORDER BY wpostmeta.meta_value DESC
";
$pageposts = $wpdb->get_results($querystr, OBJECT);

これは、すべての都市を表示する php foreach を使用したクエリです。

  <?php

 $querystr = "
 SELECT wposts.* 
 FROM $wpdb->posts wposts, $wpdb->postmeta wpostmeta
 WHERE wposts.ID = wpostmeta.post_id 
 AND wpostmeta.meta_key  = 'Country' 
 AND wpostmeta.meta_value  = 'UK' 
 AND wposts.post_type = 'post' 
 ";

 $pageposts = $wpdb->get_results($querystr, OBJECT);

 ?>
 <?php if ($pageposts): ?>

  <?php global $post; ?>
  <?php foreach ($pageposts as $post): ?>
    <?php setup_postdata($post); ?>

    <?php echo get_post_meta($post->ID, 'city', true) ?> <br />

  <?php endforeach; ?>

 <?php endif; ?>

このクエリは、すべての英国の都市をリストし、WordPress カスタム フィールド「都市」からデータを取得します。

このクエリは、次のような都市のリストを返します。

Aberdeen
Aberdeen
Aberdeen
Aberdeen
Aberdeen
Belfast
Belfast
Belfast
Belfast
Belfast
Birchington
Birmingham
Birmingham
Birmingham
Birmingham
Birmingham
Birmingham
Birmingham
Birmingham
Birmingham
Birmingham
Birmingham
Birmingham
Birmingham
Birmingham
Birmingham
Birmingham
Birmingham
Blackpool
Bournemouth
Bournemouth
Bournemouth
Bournemouth
Bournemouth
Bournemouth
Bournemouth
Bournemouth
Brighton
    Brighton

等。!!!

「都市」フィールド値の重複があることがわかるように、都市を一度表示して、次のようなリストを取得する必要があります

      Aberdeen
      Aberdeen
      Belfast
      Birchington
      Birmingham
      Blackpool
      Bournemouth
      Brighton
      Bristol

どうすればこれを入手できますか? みんなありがとう!!

4

2 に答える 2

0

SELECT の後に DISTINCT を使用します。重複を削除します

$querystr = "
    SELECT DISTINCT wposts.* 
    FROM $wpdb->posts wposts, $wpdb->postmeta wpostmeta
    WHERE wposts.ID = wpostmeta.post_id 
    AND wpostmeta.meta_key  = 'Country' 
    AND wpostmeta.meta_value  = 'UK' 
    AND wposts.post_type = 'post' 
";
于 2013-06-26T12:04:12.230 に答える
0

これを試して

 $querystr = "
 SELECT  DISTINCT meta_value  FROM $wpdb->postmeta WHERE post_id IN(     
 SELECT DISTINCT wpostmeta.post_id
 FROM $wpdb->posts wposts, $wpdb->postmeta wpostmeta
 WHERE wposts.ID = wpostmeta.post_id 
 AND wpostmeta.meta_key  = 'Country' 
 AND wpostmeta.meta_value  = 'UK' 
 AND wposts.post_type = 'post' ) AND meta_key ='city'
 ";
 $cities = $wpdb->get_results($querystr, OBJECT);

 <?php if ($cities ): ?>

  <?php global $post; ?>
  <?php foreach ($cities as $c): ?>    
    <?php echo $c->meta_value; ?> <br />    
  <?php endforeach; ?>

 <?php endif; ?>
于 2013-06-26T12:16:07.327 に答える