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 8902571
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T01:38:52+00:00 2026-06-15T01:38:52+00:00

I have the following mysql query: SELECT id, sum(views) as total_views FROM table WHERE

  • 0

I have the following mysql query:

SELECT id, sum(views) as total_views
FROM table
WHERE id IN (1,2,3)
GROUP BY id
ORDER BY total_views ASC

If only id 1,3 are found in the database, i still want id 2 to appear, with total_views being set to 0.

Is there any way to do that? This cannot use any other table.

  • 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-15T01:38:54+00:00Added an answer on June 15, 2026 at 1:38 am

    Here’s an alternative to Micheal’s solution (not a bad solution, mind you — even with “a lot” of ID’s), so long as you’re not querying against a cluster.

    create temporary table __ids (
      id int unsigned primary key
    ) engine=MEMORY;
    
    insert into __ids (id) values
      (1),
      (2),
      (3)
    ;
    
    SELECT table.id, sum(views) as total_views
    FROM __ids left join table using (id)
    GROUP BY table.id
    ORDER BY total_views ASC
    

    And if your query becomes complex, I could even conceive of it running more efficiently this way. But, if I were you, I’d benchmark this option with Michael’s ad-hoc UNION’ed table option using real data.

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

Sidebar

Related Questions

I have the following simple MySQL query, called from PHP: SELECT foo_id, SUM(number_of_guests) FROM
I have the following MySQL query: SELECT SUM(IF(num_content>3,3,num_content)) FROM ( SELECT DATE(FROM_UNIXTIME(content_timestamp)) as dt,
I have the following MySQL query statement: SELECT * FROM table1 INNER JOIN table2
I have the following mysql query: $manufacturers_query = select m.manufacturers_id, m.manufacturers_name, m.manufacturers_image, mi.manufacturers_url from
I have the following query to select users and their locations, etc from MySQL
I have the following query in mysql 5.1.41: select distinct table_schema from information_schema.tables where
I have the following query: SELECT SUM(COUNTED) AS TCOUNTED FROM (SELECT COUNTED FROM `clicks`.`t1`
I have the following MySQL query: SELECT (COUNT(id) * 5) AS total FROM content
I have the following MySQL query: $imgDimensions_query = SELECT imgWidth, imgHeight FROM ( SELECT
I have the following MySQL query and tables from which I am querying: SELECT

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.