我想创建一个包含统计信息的团队列表,因此我使用了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 ......
有人能告诉我哪里出错了吗?
答案 0 :(得分: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);
}
答案 1 :(得分: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;
}