基本上,当meta_key字段等于'location'时,我想添加一个额外的where子句,以便根据该位置进行比较。
我用Case和IF语句尝试了这个,但似乎无法让它工作,任何我出错的想法?
$query = "
SELECT wp_posts.ID, wp_posts.post_title, wp_posts.post_type,
wp_postmeta.meta_key, wp_postmeta.meta_value, wp_postmeta.post_id,
wp_term_relationships.object_id, wp_term_relationships.term_taxonomy_id,
wp_term_taxonomy.term_id,
wp_terms.term_id, wp_terms.name
FROM wp_posts
/* Join Post Meta Table */
JOIN wp_postmeta
ON wp_posts.ID = wp_postmeta.post_id
/* Join Term Relationships Table to get the taxonomy id */
JOIN wp_term_relationships
ON wp_posts.ID = wp_term_relationships.object_id
/* Join Term Taxonomy Table to get the Term ID */
JOIN wp_term_taxonomy
ON wp_term_relationships.term_taxonomy_id = wp_term_taxonomy.term_taxonomy_id
/* Finally Join to the Terms table to get the Category Name *sigh* */
JOIN wp_terms
ON wp_term_taxonomy.term_id = wp_terms.term_id
WHERE post_type = 'listing' AND
wp_postmeta.meta_key IN ('locations', 'email', 'phone-number')";
if(isset($location)) {
$query .= "
IF wp_postmeta.meta_key='location'
THEN AND wp_postmeta.meta_value LIKE '".mysql_real_escape_string($location)."'
END IF";
}
谢谢,如果我没有清楚地解释清楚,请告诉我!
答案 0 :(得分:13)
if(isset($location)) {
$query .= " AND
CASE
WHEN wp_postmeta.meta_key='location' THEN wp_postmeta.meta_key
LIKE '".mysql_real_escape_string ($location)."'
ELSE 1=1
END ";
}
答案 1 :(得分:3)
简单的解决方案是
WHERE
(meta_key = 'Location' AND Location = 'UK') OR meta_key <> 'Location'