it is possible to "group by" without losing the original rows?
Posted
by
toPeerOrNotToPeer
on Stack Overflow
See other posts from Stack Overflow
or by toPeerOrNotToPeer
Published on 2012-09-21T09:11:24Z
Indexed on
2012/09/21
9:37 UTC
Read the original article
Hit count: 163
i have a query like this:
ID | name | commentsCount
1 | mysql for dummies | 33
2 | mysql beginners guide | 22
SELECT
...,
commentsCount // will return 33 for first row, 22 for second one
FROM
mycontents
WHERE
name LIKE "%mysql%"
also i want to know the total of comments, of all rows:
SELECT
...,
SUM(commentsCount) AS commentsCountAggregate // should return 55
FROM
mycontents
WHERE
name LIKE "%mysql%"
but this one obviously returns a single row with the total.
now i want to merge these two queries in one single only,
because my actual query is very heavy to execute (it uses boolean full text search, substring offset search, and sadly lot more), then i don't want to execute it twice
is there a way to get the total of comments without making the SELECT twice?
!! custom functions are welcome !!
also variable usage is welcome, i never used them...
© Stack Overflow or respective owner