使用2个值MySQL搜索相同的列

时间:2016-03-19 18:25:36

标签: mysql wordpress select

我不确定如何说出我需要的东西,所以这里...这是允许选择类型和状态的搜索的一部分。我需要能够运行此查询以匹配来自同一mysql列meta_value的类型和状态。问题是它返回结果中所选状态的每种类型。

示例:我为类型和南卡罗来纳州选择Drag Strip我只想看南卡罗来纳州的Drag Strip,而不是南卡罗来纳州或其他任何类型的其他类型(椭圆形,道路路线等)状态。

以下是我现在所做的不起作用,我不确定该去哪里获得我需要的结果。

SELECT dmp_postmeta.meta_value, dmp_posts.post_title, dmp_posts.post_type,
       dmp_posts.post_status, dmp_posts.id 
FROM dmp_postmeta, dmp_posts
WHERE dmp_postmeta.meta_value IN ('South Carolina','Drag Strip') 
  AND dmp_posts.post_type='my_custom_tracks' 
  AND dmp_posts.post_status='publish' 
GROUP BY dmp_postmeta.post_id ORDER BY dmp_posts.post_title


meta_key          meta_value
Track_Type        Drag Strip
Track_Type        Oval
Track_Location    South Carolina
Track_Location    Texas

****** ******解决

"SELECT p.ID as id, p.post_title, p.post_type, p.post_status as post_status, MAX(CASE WHEN pm1.meta_value='".$track_type."' then pm1.meta_value ELSE NULL END) as track_type, MAX(CASE WHEN pm1.meta_value='".$track_state."' then pm1.meta_value ELSE NULL END) as track_state FROM dmp_posts p LEFT JOIN dmp_postmeta pm1 ON ( pm1.post_id = p.ID) WHERE p.post_type='my_custom_tracks' AND p.post_status='publish' GROUP BY p.ID, p.post_title ORDER BY p.post_title"

1 个答案:

答案 0 :(得分:0)

这就是我最终的工作。谢谢你的帮助。

"SELECT p.ID as id, p.post_title, p.post_type, p.post_status as post_status, MAX(CASE WHEN pm1.meta_value='".$track_type."' then pm1.meta_value ELSE NULL END) as track_type, MAX(CASE WHEN pm1.meta_value='".$track_state."' then pm1.meta_value ELSE NULL END) as track_state FROM dmp_posts p LEFT JOIN dmp_postmeta pm1 ON ( pm1.post_id = p.ID) WHERE p.post_type='my_custom_tracks' AND p.post_status='publish' GROUP BY p.ID, p.post_title ORDER BY p.post_title"