3

我想创建一个包含统计信息的团队列表,所以我使用了 2 个函数,每个函数包含 2 个不同的数据库查询:

protected function getListQueryweqw() {
    // Create a new query object.
    $db = $this->getDbo();
    $query = $db->getQuery(true);
    $query_string = '';
    $query_string .= 'a.*, COUNT(w.id) AS played_games, SUM(w.team_goals) AS goals,';
    $query_string .= "COUNT(w.match_status = 'draw') AS draw,";
    $query_string .= "COUNT(w.match_status = 'win' ) AS win,";
    $query_string .= "COUNT(w.match_status = 'lose') AS lose,";
    $query_string .= "SUM(w.team_points) AS points, SUM(w.goal_lost) AS goal_lost, w.*";

    // Select the required fields from the table.
    $query->select($query_string);
    $query->from('`#__footsal_teams` AS a');
    $query->join('LEFT', '`#__footsal_resaults` AS w on w.id_team = a.id');

    return $query;
}

protected function getListQuery() {
    // Create a new query object.
    $db = $this->getDbo();
    $query = $db->getQuery(true);
    $query_string = '';
    $query_string .= 'a.*, COUNT(SELECT id FROM `#__footsal_resaults` WHERE id_team = a.id) AS played_games, ';
    $query_string .= "SUM(SELECT team_goals FROM `#__footsal_resaults` WHERE id_team = a.id) AS goals,";
    $query_string .= "COUNT(SELECT id FROM `#__footsal_resaults` WHERE match_status = 'draw') AS draw,";
    $query_string .= "COUNT(SELECT id FROM `#__footsal_resaults` WHERE match_status = 'win') AS win,";
    $query_string .= "COUNT(SELECT id FROM `#__footsal_resaults` WHERE match_status = 'lose') AS lose,";
    $query_string .= "SUM(SELECT team_points FROM `#__footsal_resaults` WHERE id_team = a.id) AS points, SUM(SELECT goal_lost FROM `#__footsal_resaults` WHERE id_team = a.id) AS goal_lost";

    // Select the required fields from the table.
    $query->select($query_string);
    $query->from('`#__footsal_teams` AS a');

    return $query;
}

然而,这两个查询似乎都不起作用,我得到的错误的开始是:

JDatabaseMySQLi::query: 1064 ......

有人可以告诉我我哪里出错了吗?

4

2 回答 2

1

你的东西与此类似,只是做了一些调整。

protected function getListQueryweqw() {
    $db = JFactory::getDBO();
    $query = $db->getQuery(true);
    $query_string = '';
    $query_string .= 'a.*, COUNT(w.id) AS played_games, SUM(w.team_goals) AS goals,';
    $query_string .= "COUNT(w.match_status = 'draw') AS draw,";
    $query_string .= "COUNT(w.match_status = 'win' ) AS win,";
    $query_string .= "COUNT(w.match_status = 'lose') AS lose,";
    $query_string .= "SUM(w.team_points) AS points, SUM(w.goal_lost) AS goal_lost, w.*";

    $query->select($query_string);
    $query->from('#__footsal_teams AS a');
    $query->join('LEFT', '#__footsal_resaults AS w on w.id_team = a.id');


    return $db->setQuery($query);
}

protected function getListQuery() {
    $db = JFactory::getDBO();
    $query = $db->getQuery(true);
    $query_string = '';
    $query_string .= 'a.*, COUNT(SELECT id FROM #__footsal_resaults WHERE id_team = a.id) AS played_games, ';
    $query_string .= "SUM(SELECT team_goals FROM #__footsal_resaults WHERE id_team = a.id) AS goals,";
    $query_string .= "COUNT(SELECT id FROM #__footsal_resaults WHERE match_status = 'draw') AS draw,";
    $query_string .= "COUNT(SELECT id FROM #__footsal_resaults WHERE match_status = 'win') AS win,";
    $query_string .= "COUNT(SELECT id FROM #__footsal_resaults WHERE match_status = 'lose') AS lose,";
    $query_string .= "SUM(SELECT team_points FROM #__footsal_resaults WHERE id_team = a.id) AS points, SUM(SELECT goal_lost FROM #__footsal_resaults WHERE id_team = a.id) AS goal_lost";

    $query->select($query_string);
    $query->from('#__footsal_teams AS a');

    return $db->setQuery($query);
}
于 2012-11-09T14:27:56.203 回答
0

好的,我解决它

protected function getListQuery() {
    // Create a new query object.
    $db = $this->getDbo();
    $query = $db->getQuery(true);
    $query_string = '';
    $query_string .= 'a.*, (SELECT COUNT(id) FROM `#__footsal_resaults` WHERE id_team = a.id) AS played_games, ';
    $query_string .= "(SELECT SUM(team_goals) FROM `#__footsal_resaults` WHERE id_team = a.id) AS goals,";
    $query_string .= "(SELECT COUNT(id) FROM `#__footsal_resaults` WHERE match_status = 'draw' AND id_team = a.id) AS draw,";
    $query_string .= "(SELECT COUNT(id) FROM `#__footsal_resaults` WHERE match_status = 'win' AND id_team = a.id) AS win,";
    $query_string .= "(SELECT COUNT(id) FROM `#__footsal_resaults` WHERE match_status = 'lose' AND id_team = a.id) AS lose,";
    $query_string .= "(SELECT SUM(team_points) FROM `#__footsal_resaults` WHERE id_team = a.id) AS points, (SELECT SUM(goal_lost) FROM `#__footsal_resaults` WHERE id_team = a.id) AS goal_lost";

    // Select the required fields from the table.
    $query->select($query_string);
    $query->from('`#__footsal_teams` AS a');

    return $query;
}
于 2012-11-09T14:54:54.760 回答