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

The Archive Base Latest Questions

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

I would like to ask a quick question regarding a mysql query. I have

  • 0

I would like to ask a quick question regarding a mysql query.

I have a table named trans :

+----+---------------------+------+-------+----------+----------+
| ID | Date                | User | PCNum | Customer | trans_In |
+----+---------------------+------+-------+----------+----------+
|  8 | 2013-01-23 16:24:10 | test | PC2   | George   |       10 |
|  9 | 2013-01-23 16:27:22 | test | PC2   | Nick     |        0 |
| 10 | 2013-01-24 16:28:48 | test | PC2   | Ted      |       10 |
| 11 | 2013-01-25 16:36:40 | test | PC2   | Danny    |       10 |
+----+---------------------+------+-------+----------+----------+

and another named customers :

+----+---------+-----------+
| ID | Name    | Surname   |
+----+---------+-----------+
|  1 | George  |           |
|  2 | Nick    |           |
|  3 | Ted     |           |
|  4 | Danny   |           |
|  5 | Alex    |           |
|  6 | Mike    |           |
   .
   .
   .
   .
+----+---------+-----------+

I want to view the sum of trans_in column for specific customers in a date range BUT ALSO include in the result set, those customers that haven’t got any records in the selected date range. Their sum of trans_in could appear as NULL or 0 it doesn’t matter…

I have the following query :

SELECT
  `Date`,
  Customer,
  SUM(trans_in) AS 'input'
FROM trans
WHERE Customer IN('George','Nick','Ted','Danny')
    AND `Date` >= '2013-01-24'
GROUP BY Customer
ORDER BY input DESC;

But this will only return the sum for ‘Ted’ and ‘Danny’ because they only have transactions after the 24th of January…

How can i include all the customers that are inside the WHERE IN (…) function, even those who have no transactions in the selected date range??

I suppose i’ll have to join them somehow with the customers table but i cannot figure out how.

Thanks in advance!!

🙂

  • 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-18T01:52:07+00:00Added an answer on June 18, 2026 at 1:52 am

    In order to include all records from one table without matching records in another, you have to use a LEFT JOIN.

    SELECT
      t.`Date`,
      c.name,
      SUM(t.trans_in) AS 'input'
    FROM customers c LEFT JOIN trans t ON (c.name = t.Customer AND t.`Date` >= '2013-01-24')
    WHERE c.name IN('George','Nick','Ted','Danny')
    GROUP BY c.name
    ORDER BY input DESC;
    

    Of course, I would mention that you should be referencing customer by ID, and not by name in your related table. Your current setup leads to information duplication. If the customer changes their name, you now have to update all related records in the trans table instead of just in the customer table.

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

Sidebar

Related Questions

I would like to ask such question, I have XML xsd`s, which generate beans
I would like to ask a question regarding single channel image interpolation. Single channel
I would like to ask for help regarding datasets and Crystal Reports. I have
I would like to ask you: Imagine that you have a site and there
I would like to ask you about regular expressions preg_match have outlined below. I
i would like to ask a question about @UsesJAXBContext annotation in jax-ws. I try
I would like to ask for more an opinion than a question: What would
I just have a quick best practice question regarding custom cells in a UITableView.
Quick question, I am working on a project at work and I would like
I would like to ask a couple of quick questions about the proper destruction

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.