Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8572013
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T18:54:36+00:00 2026-06-11T18:54:36+00:00

i have a query like this: ID | name | commentsCount 1 | mysql

  • 0

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…

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T18:54:38+00:00Added an answer on June 11, 2026 at 6:54 pm

    One obvious solution is storing intermediate results withing another ‘temporary’ table, and than perform aggregation in the second step.

    Another solution is preparing a lookup table containing sums you need (but there obviously needs to be some grouping ID, I call it MASTER_ID), like that:

    CREATE TABLE comm_lkp AS
    SELECT MASTER_ID, SUM(commentsCount) as cnt
    FROM mycontents
    GROUP BY MASTER_ID
    

    Also create an index on that table on column MASTER_ID. Later, you can modify your query like that:

    SELECT
        ...,
        commentsCount,
        cnt as commentsSum
    FROM
        mycontents as a
            JOIN comm_lkp as b ON (a.MASTER_ID=b.MASTER_ID)
    WHERE
        name LIKE "%mysql%"
    

    It also shouldn’t touch your performance as long as lookup table will be relatively small.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have MySQL query like this SELECT `transaction`.id, CONCAT(contact.`name`, , contact.last_name) as fullName, (SELECT
I have a query like this: select empno,name from emp where job = 'CLERK'
I have a query like this SELECT TOWN, NAME FROM CINEMA WHERE CITY_ID =
i have this sql query select * from table where name like ? but
I have a query like this: SELECT name FROM mytable WHERE id = $id
I have a SQL query like this: SELECT * FROM ( (SELECT name FROM
I have a query like this: SELECT type.id, type.name, COUNT(*) AS tot FROM page
I have a query like this: SELECT Name, REPLACE(RTRIM(( SELECT CAST(Score AS VARCHAR(MAX)) +
I have a simple query like this SELECT hometeam.name AS hometeamName, hometeam.shortname AS hometeamShortName,
I have a query like this: Select E.ID, E.NAME, date_format( CONVERT_TZ( CONVERT_TZ( E.ST, '+00:00',

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.