-1

I have a query that joins multiple tables with left outer join query from SQL. I want to join a table in such a way that I get only the max id rows being cross matched with one of the other tables. Say I have a table1 that is joined with table 2 and table 3, now comes the 4th table, which also joins the 1st table but in some different way, let me explain it with an image.

enter image description here

The end result that I expect is like the image below

enter image description here

The queries that I have made bring the records when added alone but show an error when left joined with other tables.

Here is the query that brings all the records from tables but I want to get the max record by callid joined with newload id; now it brings the 1st records that are added in newcheckcall table for newload.

$mysqli->query("SELECT * 
FROM newload n 
LEFT OUTER JOIN truck_details AS t 
ON t.truckNumber = n.truck_Number 
LEFT OUTER JOIN broker_details AS b 
ON b.brokerName = n.Broker 
LEFT OUTER JOIN 
(select *, MAX(callid) from newcheckcalls GROUP BY newloadID) C ON C.newloadID = n.id
where n.status='load_en_route' 
ORDER BY id DESC ")
    or die($mysqli->error);

This query brings the records that I expect when queried alone, but when added with the above query, it shows the error.

$mysqli->query("select * from newcheckcalls C
where exists (select 1 from (select newloadID, max(callid) as callid
from newcheckcalls cc 
GROUP BY newloadID) as cc 
where C.newloadID = cc.newloadID and C.callid = cc.callid)")
    or die($mysqli->error);
philipxy
  • 14,416
  • 5
  • 32
  • 77
  • 2
    [Why should I not upload images of code/data/errors when asking a question?](https://meta.stackoverflow.com/q/285551/3404097) [Why are images of text, code and mathematical expressions discouraged?](https://meta.stackexchange.com/q/320052/266284) – philipxy May 22 '22 at 20:17
  • Please in code questions give a [mre]--cut & paste & runnable code; example input with desired & actual output (including verbatim error messages); tags & versions; clear specification & explanation. For errors that includes the least code you can give that is code that you show is OK extended by code that you show is not OK. (Debugging fundamental.) For SQL include DDL & tabular initialization code. When you get a result you don't expect, pause your overall goal, chop to the 1st subexpression with unexpected result & say what you expected & why, justified by documentation. [ask] [Help] – philipxy May 23 '22 at 08:31
  • Possible duplicate of [Fetch the row which has the Max value for a column](https://stackoverflow.com/q/121387/3404097). When pinned down by a [mre] this will be a faq. Please before considering posting read the manual/reference & google any error message & many clear, concise & precise phrasings of your question/problem/goal, with & without your particular names/strings/numbers, 'site:stackoverflow.com' & tags; read many answers. If asking reflect research. PS Saying you searched does not show research effort. Describe your searching & explain how it didn't help. PS Not images of text. – philipxy May 23 '22 at 08:33

0 Answers0