Correct Join Query for SQL Query - mysql

I currently have the following Query
SELECT * FROM tbl_Cars c
INNER JOIN tbl_Car_Parts cp ON c.Id = cp.Id
INNER JOIN tbl_Car_Factory cf ON cp.Id = cf.Id
However I have now realised that there are some Ids which are in the Cars Table which are then not in the Car Parts table so these are being omitted from my results while I want them included. I tried changing my INNER JOIN to a LEFT OUTER JOIN but it made no difference in the result set I am returned?

Use LEFT OUTER JOIN in both of the joins.
SELECT * FROM tbl_Cars c
LEFT OUTER JOIN tbl_Car_Parts cp ON c.Id = cp.Id
LEFT OUTER JOIN tbl_Car_Factory cf ON cp.Id = cf.Id
Otherwise the second INNER JOIN will invalidate your first LEFT OUTER JOIN for those records that does not have ID (does not join) in the tbl_Car_Parts table.
After a LEFT OUTER JOIN you may be only use again INNER JOIN if the table you are joining is not related with the previous ones that are joined using the LEFT OUTER JOIN.

LEFT JOIN should have definitely solved your problem, Not sure why it didnt work.
Just to be safe I used Left join on both the tables...
SELECT * FROM tbl_Cars c
LEFT JOIN tbl_Car_Parts cp ON c.Id = cp.Id
LEFT JOIN tbl_Car_Factory cf ON cp.Id = cf.Id

Related

MySQL LEFT OUTER JOIN not giving correct value

I have the following tables and relationships
Here are the sample data -
I tried the following query-
SELECT project.*, SUM(schedule.amount) as sch_total, SUM(expense.amount) as expense_total, SUM(bill.amount) as bill_total from project
LEFT OUTER JOIN schedule on project.project_id = schedule.project_id
LEFT OUTER JOIN site on project.project_id = site.project_id
LEFT OUTER JOIN expense on site.site_id = expense.site_id
LEFT OUTER JOIN bill on site.site_id = bill.site_id
GROUP BY project.project_id
This is the query result -
But "bill_total" should be 200, not 400.
What did I miss in the query?
Your expense table have multiple rows for same Id. That table must be aggregated first and then used in join -
SELECT project.*,
SUM(schedule.amount) as sch_total,
SUM(E.amount) as expense_total,
SUM(bill.amount) as bill_total
FROM project
LEFT OUTER JOIN schedule on project.project_id = schedule.project_id
LEFT OUTER JOIN site on project.project_id = site.project_id
LEFT OUTER JOIN (SELECT site_id, SUM(expense.amount)amount
FROM expense
GROUP BY site_id) E on site.site_id = expense.site_id
LEFT OUTER JOIN bill on site.site_id = bill.site_id
GROUP BY project.project_id

DELETE all using Left join of 3 tables

SELECT * FROM property
LEFT JOIN installmentagreement
ON property.PId=installmentagreement.PId
LEFT JOIN installments
ON installmentagreement.Id=installments.Id
LEFT JOIN rentagreement
ON property.PId=rentagreement.PId
WHERE property.PId=39
This is the SELECT query that works as expected. I want to DELETE the result of this SELECT query. I used this query.
DELETE FROM property( SELECT * FROM property
LEFT JOIN installmentagreement
ON property.PId=installmentagreement.PId
LEFT JOIN installments
ON installmentagreement.Id=installments.Id
LEFT JOIN rentagreement
ON property.PId=rentagreement.PId
WHERE property.PId=39)
And this also.
DELETE FROM property
LEFT JOIN installmentagreement
ON property.PId=installmentagreement.PId
LEFT JOIN installments
ON installmentagreement.Id=installments.Id
LEFT JOIN rentagreement
ON property.PId=rentagreement.PId
WHERE property.PId=39
But it returns with Syntax Error. I want to covert the SELECT query into DELETE query. The DB I am using is MySQL.If there is any other better solution please suggest. Please help!
I would recommend using aliases for clarity and more:
SELECT *
FROM property as p
LEFT JOIN installmentagreement as ia
ON p.PId=ia.PId
LEFT JOIN installments as i
ON ia.Id=i.Id
LEFT JOIN rentagreement as ra
ON p.PId=ra.PId
WHERE p.PId=39
Then you can convert easily to delete statement as follows:
delete p
FROM property as p
LEFT JOIN installmentagreement as ia
ON p.PId=ia.PId
LEFT JOIN installments as i
ON ia.Id=i.Id
LEFT JOIN rentagreement as ra
ON p.PId=ra.PId
WHERE p.PId=39
And if you wanted to delete records from some of the left-joined tables before you delete the property records, you can just as easily change the alias in the delete statement:
delete ra
FROM property as p
LEFT JOIN installmentagreement as ia
ON p.PId=ia.PId
LEFT JOIN installments as i
ON ia.Id=i.Id
LEFT JOIN rentagreement as ra
ON p.PId=ra.PId
WHERE p.PId=39
MySQL supports deleting from multiple tables in one statement, but you need to specify the tables you want to delete from:
DELETE p, ima, i, ra
FROM property p LEFT JOIN
installmentagreement ima
ON p.PId = ima.PId LEFT JOIN
installments i
ON ima.Id = i.Id LEFT JOIN
rentagreement ra
ON p.PId = ra.PId
WHERE p.PId = 39;
Of course, you don't have to include all four tables after the DELETE, just the ones you want to delete from.

In MySQL JOIN means which join perform..?

It's to confusing, I have query where I have write JOIN with multiple table then which type of join it'll perform..?
For example :
SELECT
b.*
FROM
tbl_bookings b
JOIN tbl_users ua ON ua.id = b.act_id
JOIN tbl_users uc ON uc.id = b.cust_id
JOIN tbl_venue v ON b.venue_id = v.venue_id
WHERE
b.act_id = 4
Can any one please let me know by which type of join it'll perform..?
JOIN equals to an INNER JOIN . They are functionally equivalent, but INNER JOIN can be a bit clearer to read, especially if the query contains other type of JOIN
For something other than INNER JOIN you should specify the join you want.
LEFT JOIN / RIGHT JOIN which are the same as LEFT OUTER JOIN and RIGHT OUTER JOIN .
Those are just different ways of saying the same thing.
It will perform INNER JOIN. It is good to write INNER JOIN when you have different types of joins in query.

MS Access 2013 SQL Query LEFT JOIN

FROM (((Project
INNER JOIN MDS ON Project.PID=MDS.PID)
INNER JOIN PLocation ON Project.PID = PLocation.PID)
INNER JOIN Site ON PLocation.ACode = Site.ACode) AS [Prim]
LEFT JOIN ((Procurement INNER JOIN MagicT ON Procurement.PRNum = MagicT.PRNum)
INNER JOIN DO ON DO.DoNum = MagicT.DONum) AS [Prim2] ON Prim.PRNum = Prim2.PRNum
Hey guys. So the above from statement is giving me an error:
Syntax error in from clause
Funny thing is when I make the LEFT JOIN an INNER JOIN it runs fine, but sadly that isn't what I want. I read that I had to rename the inner queries and join them using their new names but unfortunately I do not think I did it properly.
Maybe this... using two inline views. but you should be able to do this with just ()'s in Access... however if you have same named columns in each of Prim/prim2 tables, this will present with errors you may have to spell out each of the column names needed in the inline views.
FROM (SELECT * FROM Project
INNER JOIN MDS ON Project.PID=MDS.PID
INNER JOIN PLocation ON Project.PID = PLocation.PID
INNER JOIN Site ON PLocation.ACode = Site.ACode) PRIM
LEFT JOIN (SELECT * from Procurement
INNER JOIN MagicT ON Procurement.PRNum = MagicT.PRNum
INNER JOIN DO ON DO.DoNum = MagicT.DONum) PRIM2
ON Prim.PRNUM = Prim2.PRNUM
--- maybe... in access the ()'s will handle the outer join but I don't know what table PRNUM is sourced from in (project, mds, plocation,site) so project is a guess...
FROM Project
INNER JOIN MDS ON Project.PID=MDS.PID
INNER JOIN PLocation ON Project.PID = PLocation.PID
INNER JOIN Site ON PLocation.ACode = Site.ACode
LEFT JOIN (SELECT * from Procurement
INNER JOIN MagicT ON Procurement.PRNum = MagicT.PRNum
INNER JOIN DO ON DO.DoNum = MagicT.DONum) PRIM2
ON Project.PRNUM = Prim2.PRNUM
You basically have
FROM Subquery1 LEFT JOIN Subquery2
but your subqueries don't have SELECT in them.
I think it should be:
FROM
(SELECT Prim.PRNum FROM
(((Project
INNER JOIN MDS ON Project.PID=MDS.PID)
INNER JOIN PLocation ON Project.PID = PLocation.PID)
INNER JOIN Site ON PLocation.ACode = Site.ACode)
AS [Prim])
LEFT JOIN
(SELECT Prim2.PRNum FROM
((Procurement INNER JOIN MagicT ON Procurement.PRNum = MagicT.PRNum)
INNER JOIN DO ON DO.DoNum = MagicT.DONum)
AS [Prim2])
ON Prim.PRNum = Prim2.PRNum
(edit: a little too late...)
I have also discovered one of my issues here, is that in Access, you can have a left or right join nested inside an inner join but CANNOT have an inner join inside a left or right join. Rewriting the query to not include these joins inside the inner joins has solved the issue.
Thank you for all the help.

Left outer join in access?

Following query does not work in access.
SELECT Fields.FieldId, PrecisionSettings.DecimalPlaces
from Fields left outer join FieldGroup on Fields.FieldGroupId = FieldGroup.FieldGroupId
left outer join Category on FieldGroup.CategoryId = Category.CategoryId
left outer join PrecisionSettings on
Category.InputAttributesID=PrecisionSettings.AttributesID
It gives error as missing operator in query expression.
In Access you can only join two results at a time. To join more tables you need more parentheses:
SELECT
Fields.FieldId,
PrecisionSettings.DecimalPlaces
FROM
(
(
Fields
LEFT OUTER JOIN FieldGroup ON Fields.FieldGroupId = FieldGroup.FieldGroupId
)
LEFT OUTER JOIN Category ON FieldGroup.CategoryId = Category.CategoryId
)
LEFT OUTER JOIN PrecisionSettings ON Category.InputAttributesID = PrecisionSettings.AttributesID