gpt4 book ai didi

mysql - 每个表使用 2 个值连接 AS 别名值

转载 作者:行者123 更新时间:2023-11-29 08:34:39 24 4
gpt4 key购买 nike

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");

最佳答案

查询无法知道您想要连接特定行的哪个元值,事实上在当前查询中这是不可能的(即它一次只能连接一个)。您需要加入 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')

这就是您需要做的要点;只需添加其他连接和条件即可。

关于mysql - 每个表使用 2 个值连接 AS 别名值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15550810/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com