4
SELECT b.post_title, a.post_id, COUNT( * ) as Total
FROM  wp_posts b INNER JOIN 
            wp_postmeta a ON a.post_id = b.ID
    WHERE a.meta_value = 1
      AND a.meta_key = 'type-select' 
      AND b.post_status = 'publish'
      and post_type = 'car-cc'
GROUP BY b.post_title, a.post_id

目前它选择了post titlepost id但我还需要选择一个meta valuewhere meta key = type-gen问题是另一个元键已经在查询中进行了比较。

SQL 小提琴:http ://sqlfiddle.com/#!2/109c2/1

4

3 回答 3

3

干得好

SELECT b.post_title, a.post_id, COUNT( * ) AS Total,
(SELECT meta_value FROM  `wp_postmeta` WHERE post_id= b.ID AND meta_key='type-gen') AS 'new  meta value'
FROM  wp_posts b INNER JOIN 
            wp_postmeta a ON a.post_id = b.ID
    WHERE a.meta_value = 1
      AND a.meta_key = 'type-select' 
      AND b.post_status = 'publish'
      AND post_type = 'car-cc'
GROUP BY b.post_title, a.post_id

小提琴

于 2013-07-24T14:00:54.947 回答
1
SELECT b.post_title, a.post_id, COUNT( * ) as Total
FROM  wp_posts b INNER JOIN 
            wp_postmeta a ON a.post_id = b.ID
    WHERE (a.meta_value = 1
      AND a.meta_key = 'type-select' 
      AND b.post_status = 'publish'
      and post_type = 'car-cc') 
    OR (a.meta_value = 1
      AND a.meta_key = 'type-gen' 
      AND b.post_status = 'publish'
      and post_type = 'car-cc')
GROUP BY b.post_title, a.post_id
于 2013-07-24T13:32:59.687 回答
1

为此使用WP_Query

Codex 页面修改

$args = array(
  'post_type' => 'car-cc',
  'meta_query' => array(
    array(
      'key' => 'type-select',
      'value' => '1',
      'compare' => '='
),
    array(
      'key' => 'type-gen',
      'value' => 'my-great-type',
      'compare' => 'LIKE'
    )
  )
);

$query = new WP_Query( $args );
于 2013-07-24T15:26:43.737 回答