Skip to content

Commit d844e66

Browse files
committed
Query HAVING should after GROUP BY #1262
1 parent bd71a9d commit d844e66

File tree

2 files changed

+20
-12
lines changed

2 files changed

+20
-12
lines changed

packages/query/src/Grammar/AbstractGrammar.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -149,14 +149,14 @@ public function compileSelect(Query $query): string
149149
$sql['where'] = $where;
150150
}
151151

152-
if ($having = $query->getHaving()) {
153-
$sql['having'] = $having;
154-
}
155-
156152
if ($group = $query->getGroup()) {
157153
$sql['group'] = $group;
158154
}
159155

156+
if ($having = $query->getHaving()) {
157+
$sql['having'] = $having;
158+
}
159+
160160
// Only order and limit can after union
161161
if ($order = $query->getOrder()) {
162162
$sql['order'] = $order;

packages/query/test/QueryTest.php

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1109,10 +1109,12 @@ public function testWhereVariant()
11091109
->whereNotIn('created', [55, 66])
11101110
->whereNotLike('content', '%qwe%');
11111111

1112-
self::assertSqlEquals(
1113-
'SELECT * FROM "foo" WHERE "id" IN (1, 2, 3) AND "id" NOT IN (5, 6, 7) '
1114-
. 'AND "time" BETWEEN \'2012-03-30\' AND \'2020-02-24\' '
1115-
. 'AND "created" NOT IN (55, 66) AND "content" NOT LIKE \'%qwe%\'',
1112+
self::assertSqlFormatEquals(
1113+
<<<SQL
1114+
SELECT * FROM "foo" WHERE "id" IN (1, 2, 3) AND "id" NOT IN (5, 6, 7)
1115+
AND "time" BETWEEN '2012-03-30' AND '2020-02-24'
1116+
AND "created" NOT IN (55, 66) AND "content" NOT LIKE '%qwe%'
1117+
SQL,
11161118
$q->render(true)
11171119
);
11181120

@@ -1122,11 +1124,17 @@ public function testWhereVariant()
11221124
->havingIn('id', [1, 2, 3])
11231125
->havingBetween('time', '2012-03-30', '2020-02-24')
11241126
->havingNotIn('created', [55, 66])
1125-
->havingNotLike('content', '%qwe%');
1127+
->havingNotLike('content', '%qwe%')
1128+
->group('time')
1129+
->order('id');
11261130

1127-
self::assertSqlEquals(
1128-
'SELECT * FROM "foo" HAVING "id" IN (1, 2, 3) AND "time" BETWEEN \'2012-03-30\' AND \'2020-02-24\' '
1129-
. 'AND "created" NOT IN (55, 66) AND "content" NOT LIKE \'%qwe%\'',
1131+
self::assertSqlFormatEquals(
1132+
<<<SQL
1133+
SELECT * FROM "foo" GROUP BY "time"
1134+
HAVING "id" IN (1, 2, 3) AND "time" BETWEEN '2012-03-30'
1135+
AND '2020-02-24' AND "created" NOT IN (55, 66) AND "content" NOT LIKE '%qwe%'
1136+
ORDER BY "id"
1137+
SQL,
11301138
$q->render(true)
11311139
);
11321140
}

0 commit comments

Comments
 (0)