Fix incorrect handling of subquery in some SELECT DISTINCT queries

This commit is contained in:
Kijin Sung 2021-01-07 01:05:32 +09:00
parent 3f766a937c
commit cf1f4f3a3b

View file

@ -115,6 +115,9 @@ class Query extends VariableBase
*/
protected function _getSelectQueryString(bool $count_only = false): string
{
// Initialize the query string.
$result = 'SELECT';
// Compose the column list.
if ($this->_column_list)
{
@ -152,19 +155,26 @@ class Query extends VariableBase
if ($count_only)
{
$count_wrap = ($this->groupby || $this->select_distinct || preg_match('/\bDISTINCT\b/i', $column_list));
if ($count_wrap && ($column_list === '*' || preg_match('/\\.\\*/', $column_list)))
if ($count_wrap)
{
$result = 'SELECT 1';
if ($column_list === '*' || preg_match('/\\.\\*/', $column_list))
{
$result .= ' 1';
}
else
{
$result .= ($this->select_distinct ? ' DISTINCT ' : ' ') . $column_list;
}
}
else
{
$result = 'SELECT COUNT(*) AS `count`';
$result .= ' COUNT(*) AS `count`';
}
}
else
{
$count_wrap = false;
$result = 'SELECT ' . ($this->select_distinct ? 'DISTINCT ' : '') . $column_list;
$result .= ($this->select_distinct ? ' DISTINCT ' : ' ') . $column_list;
}
// Compose the FROM clause.