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");
不起作用的部分是让城市和州进入结果。当前查询将城市放入 city 和 state 列。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");