Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 22 additions & 2 deletions querydefs.go
Original file line number Diff line number Diff line change
Expand Up @@ -195,11 +195,30 @@ func queryString(tq *SQuery, tmpFields ...IQueryField) string {
}
if tq.groupBy != nil && len(tq.groupBy) > 0 {
buf.WriteString(" GROUP BY ")
for i, f := range tq.groupBy {
groupByFields := make(map[string]IQueryField)
for i := range tq.groupBy {
f := tq.groupBy[i]
if _, ok := groupByFields[f.Reference()]; ok {
continue
}
if i > 0 {
buf.WriteString(", ")
}
buf.WriteString(f.Reference())
groupByFields[f.Reference()] = f
}
// DAMENG SQL Compatibility, all order by fields should be in group by
for i := range tq.orderBy {
f := tq.orderBy[i]
if _, ok := groupByFields[f.field.Reference()]; ok {
continue
}
if ff, ok := f.field.(IFunctionQueryField); ok && ff.IsAggregate() {
continue
}
buf.WriteString(", ")
buf.WriteString(f.field.Reference())
groupByFields[f.field.Reference()] = f.field
}
}
/*if tq.having != nil {
Expand All @@ -208,7 +227,8 @@ func queryString(tq *SQuery, tmpFields ...IQueryField) string {
}*/
if tq.orderBy != nil && len(tq.orderBy) > 0 {
buf.WriteString(" ORDER BY ")
for i, f := range tq.orderBy {
for i := range tq.orderBy {
f := tq.orderBy[i]
if i > 0 {
buf.WriteString(", ")
}
Expand Down