gpt4 book ai didi

mysql - 与 phpmyadmin 相比,$wpdb->get_results 查询返回不同(错误)的结果

转载 作者:行者123 更新时间:2023-11-29 07:32:50 24 4
gpt4 key购买 nike

我的数据库中有 4 个自定义表 - wp_api_teamswp_api_matcheswp_api_competitionswp_api_federations。它们是我的 Wordpress 数据库的一部分,这就是它们的外观(我正在使用外键和所有这些):

phpmyadmin schema

我正在尝试输出属于联合会的所有团队。当我在 phpmyadmin 中运行它时,它起作用了:

SELECT
wp_api_teams.team_id,
wp_api_teams.title,
wp_api_competitions.comp_id,
wp_api_competitions.title,
wp_api_federations.federation_id,
wp_api_federations.title
FROM
wp_api_teams
JOIN wp_api_matches
ON wp_api_teams.team_id = wp_api_matches.hometeam_id OR wp_api_teams.team_id = wp_api_matches.awayteam_id
JOIN wp_api_competitions
ON wp_api_matches.comp_id = wp_api_competitions.comp_id
JOIN wp_api_federations
ON wp_api_competitions.federation_id = wp_api_federations.federation_id
WHERE wp_api_federations.federation_id = 1
GROUP BY (wp_api_teams.team_id);

我得到了我正在寻找的确切结果。

-----------------------------------------------------------------------------------
| team_id | title | comp_id | title | federation_id | title |
1 Arsenal 1 Premier League 1 England
2 Chelsea 1 Premier League 1 England
3 Liverpool 1 Premier League 1 England
4 Manchester United 1 Premier League 1 England
5 Manchester City 1 Premier League 1 England

这是我在 Wordpress 中使用的代码:

global $wpdb;
$sql = "
SELECT
wp_api_teams.team_id,
wp_api_teams.title,
wp_api_competitions.comp_id,
wp_api_competitions.title,
wp_api_federations.federation_id,
wp_api_federations.title
FROM
wp_api_teams
JOIN wp_api_matches
ON wp_api_teams.team_id = wp_api_matches.hometeam_id OR wp_api_teams.team_id = wp_api_matches.awayteam_id
JOIN wp_api_competitions
ON wp_api_matches.comp_id = wp_api_competitions.comp_id
JOIN wp_api_federations
ON wp_api_competitions.federation_id = wp_api_federations.federation_id
WHERE wp_api_federations.federation_id = 1
GROUP BY wp_api_teams.team_id
";
$test = $wpdb->get_results($sql);
echo('<pre>');
print_r($test);
echo('</pre>');

我得到的结果缺少两个表列:

Array
(
[0] => stdClass Object
(
[team_id] => 1
[title] => England
[comp_id] => 1
[federation_id] => 1
)
[1] => stdClass Object
(
[team_id] => 2
[title] => England
[comp_id] => 1
[federation_id] => 1
)

....等等。

如您所见,结果中缺少 wp_api_teams.titlewp_api_competitions.title 列。

所以我的问题是,为什么会这样,为什么这些列被省略了,我能做些什么来解决这个问题,因为我在返回的结果中绝对需要它们。相同的查询如何在 WP 和 PHPmyadmin 中产生不同的结果。

这是已知错误还是我的代码有问题?

我在 localhost 上运行 10.2.14-MariaDB,所有表引擎都是 InnoDB

最佳答案

结果集中的列名相同。您需要提供一个别名:

global $wpdb;
$sql = "
SELECT
wp_api_teams.team_id,
wp_api_teams.title as team_title,
wp_api_competitions.comp_id,
wp_api_competitions.title as competition_title,
wp_api_federations.federation_id,
wp_api_federations.title as federation_title
FROM
wp_api_teams
JOIN wp_api_matches
ON wp_api_teams.team_id = wp_api_matches.hometeam_id OR wp_api_teams.team_id = wp_api_matches.awayteam_id
JOIN wp_api_competitions
ON wp_api_matches.comp_id = wp_api_competitions.comp_id
JOIN wp_api_federations
ON wp_api_competitions.federation_id = wp_api_federations.federation_id
WHERE wp_api_federations.federation_id = 1
GROUP BY wp_api_teams.team_id
";
$test = $wpdb->get_results($sql);
echo('<pre>');
print_r($test);
echo('</pre>');

关于mysql - 与 phpmyadmin 相比,$wpdb->get_results 查询返回不同(错误)的结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50418136/

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