$wpdb->get\u results查询返回的结果与phpmyadmin不同(错误)

zmeyuzjn  于 2021-06-21  发布在  Mysql
关注(0)|答案(1)|浏览(285)

我的数据库中有4个自定义表- wp_api_teams , wp_api_matches , wp_api_competitions 以及 wp_api_federations . 它们是我的wordpress数据库的一部分,这就是它们的外观(我使用外键和所有这些):

我正在努力输出所有属于联盟的团队。当我在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.title 以及 wp_api_competitions.title 结果中缺少列。
所以我的问题是,为什么会发生这种情况,为什么这些列会被省略,我能做些什么来解决这个问题,因为我在返回的结果中绝对需要它们。同一个查询如何在wp和phpmyadmin中产生不同的结果。
这是已知的错误还是我的代码有问题?
我在跑步 10.2.14-MariaDBlocalhost 所有的table引擎都是 InnoDB .

muk1a3rh

muk1a3rh1#

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

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

相关问题