Note that there are some explanatory texts on larger screens.

plurals
  1. POMysql include column with no rows returned for specific dates
    primarykey
    data
    text
    <p>I would like to ask a quick question regarding a mysql query.</p> <p>I have a table named trans : </p> <pre><code>+----+---------------------+------+-------+----------+----------+ | 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 | +----+---------------------+------+-------+----------+----------+ </code></pre> <p>and another named customers :</p> <pre><code>+----+---------+-----------+ | ID | Name | Surname | +----+---------+-----------+ | 1 | George | | | 2 | Nick | | | 3 | Ted | | | 4 | Danny | | | 5 | Alex | | | 6 | Mike | | . . . . +----+---------+-----------+ </code></pre> <p>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...</p> <p>I have the following query : </p> <pre><code>SELECT `Date`, Customer, SUM(trans_in) AS 'input' FROM trans WHERE Customer IN('George','Nick','Ted','Danny') AND `Date` &gt;= '2013-01-24' GROUP BY Customer ORDER BY input DESC; </code></pre> <p>But this will only return the sum for 'Ted' and 'Danny' because they only have transactions after the 24th of January...</p> <p>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??</p> <p>I suppose i'll have to join them somehow with the customers table but i cannot figure out how.</p> <p>Thanks in advance!!</p> <p>:)</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload