简体   繁体   中英

Joining two Mysql tables and grouping columns

I have a small problem with getting my required result in mysql.

I have two tables as follows(only the relevant columns are included):

CREATE TABLE vehicle_log
(id int(11) NOT NULL
,date date NOT NULL
,fuel_taken varchar(40) NOT NULL
,vehicle varchar(6) NOT NULL )

CREATE TABLE fuel_log
(id int(11) NOT NULL
,date date NOT NULL
,fuel_taken varchar(40) NOT NULL
,vehicle varchar(6) NOT NULL 
)

My problem is to get a result which returns a table grouping the vehicle and date columns, whilst also giving a sum of the two other fuel_amount columns like so:

date       vehicle fuel_by_receipt fuel_by_log
2016-01-01 KKK222              150        NULL
2016-01-01 KKK252              100         150
2016-01-31 KKK222              120        NULL
2016-02-10 KKK252              100        NULL
2016-02-15 KKK252               50        NULL
2016-11-10 KKK252               20        NULL
2016-11-17 KKK252               10        NULL
2016-01-31 KKK252             NULL          90

The usage would be to compare the fuel_amount columns in each table for a specific vehicle and date to see if there have been any differences and alert the user. That would be done in php, but is not the subject of this question.

Also, I would need to have the entries returned so that it needs to be within a date range + the last entry of the previous month. (I already have the month, previous month calculation), I just don't know how to include it in the query.

I was thinking of using a WHERE clause so that:

WHERE date <= end_date AND date >= start_date

But I seem to have a problem with selecting only the last entry from the last month.

Here is what I have so far:

 SELECT f.date
      , f.vehicle
      , SUM(f.fuel_taken) fuel_by_receipt
      , SUM(v.fuel_taken) fuel_by_log
   FROM fuel_log  f
   LEFT 
   JOIN vehicle_log  v
     ON v.vehicle = f.vehicle 
    AND v.date = f.date  
  GROUP 
     BY f.date
      , f.vehicle
      , v.date     #<-- THIS LINE IS REDUNDANT
      , v.vehicle  #<-- AND SO IS THIS 

 UNION 

 SELECT f.date
      , f.vehicle
      , SUM(f.fuel_taken) fuel_by_receipt
      , SUM(v.fuel_taken) fuel_by_log 
   FROM fuel_log f
  RIGHT            #<-- NOBODY EVER USES RIGHT JOIN
   JOIN vehicle_log v
     ON v.vehicle = f.vehicle 
    AND v.date = f.date  
  GROUP 
     BY f.date
      , f.vehicle
      , v.date     #<- THIS IS REDUNDANT
      , v.vehicle  #<- AND SO IS THIS

It may be that I am approaching this from a very bad angle, so if you have any suggestions to use a much better and easier approach, I would be very glad.

Thanks for your help in advance.

pat

You are interested in comparing date vehicle combinations between the two tables. Therefore you should first obtain the SUM's grouped per date and vehicle of one and JOIN it on the grouped date vehicle SUMS of the other. So not joining first on the individual records of the tables but on the grouped SUMs.

Before doing this you should first obtain all possible data/vehicle combinations (by selecting the date en vehicle values of both tables in a UNION and applying a DISTINCT). JOIN the result of the above operation with this table on the date/vehicle fields. Hope this gets you inspired!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM