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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T01:50:39+00:00 2026-05-24T01:50:39+00:00

I’ve been struggling with this query for a few days now. I’m using PHP/MySQL.

  • 0

I’ve been struggling with this query for a few days now. I’m using PHP/MySQL.

It’s for a system where customers can send freight for either collection or delivery by the company for which I am developing for. The query I am trying to write will gather the collections and deliveries carried out for a customer, and send them a summary of what they have spent that day.

I have two result sets below which are what I need to “join together” in order to send the report.

(Don’t worry about the specifics, it’s the join that is the issue 🙂 )

Deliveries Result Set

This result is a list (grouped by day) of delivery totals that the company has delivered on behalf of the customers listed.

summary_day, num_cons, num_spaces, customer_code 
2011-07-20      1       3           COB001P
2011-07-20      1       3           FAI001P
2011-07-20      2       2           FRE001P
2011-07-20      2       2           MIN001P
2011-07-20      17      29          NOR001P
2011-07-20      50      79          PAL001P
2011-07-20      1       1           PAR001P
2011-07-20      1       1           POT002P
2011-07-20      6       7           RHY001P
2011-07-20      9       13          TDG001P
2011-07-20      18      23          UPN001P

Collections Result Set

Similar to above, but in reverse, this result is a list of totals that the company has collected on behalf of each customer.

2011-07-20  9   15  ARR001P
2011-07-20  1   1   BAC002P
2011-07-20  1   1   BLA001P
2011-07-20  4   6   CAR003P
2011-07-20  2   2   DIS001P
2011-07-20  2   2   DOV001P
2011-07-20  1   1   DRY001P
2011-07-20  1   1   ECC001P
2011-07-20  3   5   FAI001P
2011-07-20  2   2   INV001P
2011-07-20  2   2   MIN001P
2011-07-20  3   3   PAL001P
2011-07-20  1   1   QUA002P
2011-07-20  1   1   TEC002P
2011-07-20  1   1   THE006P
2011-07-20  7   7   WIL005P

The Problem

I’m struggling with the JOIN to use to merge these two result sets.

Ideally the end result set should be pretty standard, with the columns of:

summary_day, customer_code, num_deliveries, num_collections

The number fields will be the num_spaces column from each result set. If a customer has both collection and delivery records, then show both the numbers. If they have one not the other, I have used COALESCE to expect one column to be NULL, and to set it to 0 accordingly.

I have tried to use a RIGHT OUTER JOIN, ON the customer_code field, in the hope that this would produce the results expected, but the only results I get are:

2011-07-20, ARR001P, 0, 15
2011-07-20, BAC002P, 0, 1
2011-07-20, BLA001P, 0, 1
2011-07-20, CAR003P, 0, 6
2011-07-20, DIS001P, 0, 2
2011-07-20, DOV001P, 0, 2
2011-07-20, DRY001P, 0, 1
2011-07-20, FAI001P, 3, 5
2011-07-20, INV001P, 0, 2
2011-07-20, MIN001P, 2, 2
2011-07-20, PAL001P, 79, 3
2011-07-20, QUA002P, 0, 1
2011-07-20, TEC002P, 0, 1
2011-07-20, THE006P, 0, 1
2011-07-20, WIL005P, 0, 7

As you can see, the result set only seems to be returning the customers who have collections, which is OK but I also need to see the customers who have deliveries and no collections.

For example, an example would be the customer NOR001P, who is in the deliveries result set but not the collections resultset…

Is a FULL OUTER JOIN required in this scenario? If so, how could I work round this considering MySQL does not support FULL OUTER JOINs?

Thanks for taking the time to read.

FULL SOLUTION

Thanks to the answer from CResults, the full solution was as follows… as you can see the delivery and collection result sets were actually made up from sub queries, so this was a bit of a head ache!

set @summary_day = '2011-07-20';
select summary_day, customer_code, sum(num_deliveries) as pallet_deliveries, sum(num_collections) as pallet_collections
from
(
        select d.summary_day, d.customer_code, d.num_spaces as num_deliveries, 0 as num_collections from
            (select 
                        @summary_day as summary_day, /* change variable */
                        count(*) as num_cons,
                        sum( coalesce(micro_qty,0) + coalesce(quarter_qty,0) + coalesce(half_qty,0) + coalesce(full_qty,0) + coalesce(ceil(vlu_qty),0) ) as num_spaces,
                        pc.customer_code 
                    from pallet_routes pr
                    inner join pallet_consignments pc on pc.route_id = pr.route_id
                    where pr.date = @summary_day /* today */
                    and pc.type = 'D'
                    group by customer_code
            ) as d
        union all
        select c.summary_day, c.customer_code, 0 as num_deliveries, num_spaces as num_collections from
            (select 
                    @summary_day as summary_day, /* change variable */
                    count(*) as num_cons,
                    sum(coalesce(micro_qty,0) + coalesce(quarter_qty,0) + coalesce(half_qty,0) + coalesce(full_qty,0) + coalesce(ceil(vlu_qty),0)) as num_spaces,
                    pc.customer_code 
                from pallet_routes pr
                inner join pallet_consignments pc on pc.route_id = pr.route_id
                where pr.date = DATE_SUB(@summary_day, INTERVAL 1 DAY) /* Yesterday */
                and pc.type = 'C'
                group by customer_code
            ) as c
) as pallet_summaries
group by summary_day, customer_code
  • 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-05-24T01:50:39+00:00Added an answer on May 24, 2026 at 1:50 am

    Totally untested, but give this a try

    Select 
        Date, CustCode, Sum(Num_Cons) as Num_Cons, Sum(Num_Cols) as Num_Cols
    from 
    (    Select Date, CustCode, Num_Cons, 0 as Num_Cols From Consignments
        UNION ALL
        Select Date, CustCode, 0 as Num_Cons, Num_Cols From Collections
    ) parcels
    group by Date, CustCode
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a jquery bug and I've been looking for hours now, I can't
this is what i have right now Drawing an RSS feed into the php,
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
Does anyone know how can I replace this 2 symbol below from the string
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I want to count how many characters a certain string has in PHP, but
I would like to count the length of a string with PHP. The string

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.