SELECT
`wp_posts`.`post_title`
, `wp_posts`.`post_content`
, `wp_posts`.`post_type`
, `wp_posts`.`ID`
, `wp_posts`.`post_date`
, `wp_usermeta`.`user_id` AS `userID`
, `wp_usermeta`.`meta_value` AS `city`
, `wp_usermeta`.`meta_value` AS `state`
, `wp_users`.`fbconnect_userid` AS `fbuid`
FROM `wp_posts`
INNER JOIN `wp_emu_shop_reviews`
ON (`wp_posts`.`ID` = `wp_emu_shop_reviews`.`postID`)
LEFT JOIN `wp_users`
ON (`wp_emu_shop_reviews`.`wpUserID` = `wp_users`.`ID`)
LEFT JOIN `wp_usermeta`
ON (`wp_users`.`ID` = `wp_usermeta`.`user_id` AND `wp_usermeta`.`meta_key` ='location_city')
OR(`wp_users`.`ID` = `wp_usermeta`.`user_id` AND `wp_usermeta`.`meta_key` ='location_state')
WHERE (`wp_posts`.`post_type` = "review"
AND `wp_posts`.`post_status` = "publish");
这部分不起作用的是让城市和州进入结果。当前查询将城市放入城市和州列。 Obv,最后一次加入是问题。
"This is the title" "blahblahblah" "review" "2074" "2012-05-03 00:03:10" "15" "Redmond" "Redmond" "xxxxxxxx"
我很可能需要从wp_usermeta表中获取更多值,但我确信如果有人为我提出这个问题,我可以弄明白。
啊..根据Juris Malinens和Explosion Pills的建议,我认为这个查询有效。
SELECT
`wp_posts`.`post_title`
/* , `wp_posts`.`post_content` */
, `wp_posts`.`post_type`
, `wp_posts`.`ID`
, `wp_posts`.`post_date`
/* , `wp_usermeta`.`user_id` AS `userID` */
, um1.user_id AS `userID`
, um1.meta_value AS `city`
, um2.meta_value AS `state`
, `wp_users`.`fbconnect_userid` AS `fbuid`
FROM `wp_posts`
INNER JOIN `wp_emu_shop_reviews`
ON (`wp_posts`.`ID` = `wp_emu_shop_reviews`.`postID`)
LEFT JOIN `wp_users`
ON (`wp_emu_shop_reviews`.`wpUserID` = `wp_users`.`ID`)
/* LEFT JOIN `wp_usermeta`
ON (`wp_users`.`ID` = `wp_usermeta`.`user_id` AND `wp_usermeta`.`meta_key` ='location_city') */
LEFT JOIN wp_usermeta um1 ON (`wp_users`.`ID` = `um1`.`user_id` AND um1.meta_key ='location_city')
LEFT JOIN wp_usermeta um2 ON(`wp_users`.`ID` = `um2`.`user_id` AND um2.meta_key ='location_state')
WHERE (`wp_posts`.`post_type` = "review"
AND `wp_posts`.`post_status` = "publish");
答案 0 :(得分:2)
查询无法知道您想要为特定行加入哪个元值,实际上在当前查询中它是不可能的(即它一次只能加入一个)。您需要加入wp_usermeta
两次:
SELECT
state.user_id AS userID
state.meta_value AS state,
city.meta_value AS city,
FROM
wp_users
JOIN wp_usermeta state ON (wp_users.ID = state.user_id
AND state.meta_key = 'location_state')
JOIN wp_usermeta city ON (wp_users.ID = city.user_id
AND state.meta_key = 'location_city')
这是你需要做的事情的要点;只需添加其他联接和条件。