Using ight join and inner join in Access - ms-access

I want to use inner join and right join statement.
This is the relation of my tables.
I'm trying but it says join statement is not supported.
Here's my code:
SELECT ProjectName, HoursWorked, FirstName, LastName
FROM (PROJECT AS P INNER JOIN ASSIGNMENT AS A ON P.ProjectID = A.ProjectID)
RIGHT JOIN EMPLOYEE AS E ON A.EmployeeNumber = E.EmployeeNumber
Someone use right join statement iteratively but it didnt' work for me.
Ms API says both left and right join can also use with inner join.
Why it didn't work?

Try using RIGHT JOIN instead of INNER JOIN in the sub select:
SELECT ProjectName, HoursWorked, FirstName, LastName
FROM (PROJECT AS P RIGHT JOIN ASSIGNMENT AS A ON P.ProjectID = A.ProjectID)
RIGHT JOIN EMPLOYEE AS E ON A.EmployeeNumber = E.EmployeeNumber
From the documentation:
A LEFT JOIN or a RIGHT JOIN may be nested inside an INNER JOIN, but an INNER JOIN may not be nested inside a LEFT JOIN or a RIGHT JOIN.

Related

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.

outer join more than 2 tables in access

After following this thread. I am getting an error as "Syntax Error in JOIN Operation"
Here Is my query
SELECT
StudentMaster.Student_id,
ExamDetails.Subject_id,
SubjectMaster.Subject_id
FROM
(StudentMaster
LEFT OUTER JOIN ExamDetails
ON StudentMaster.Student_id = ExamDetails.Student_id)
LEFT OUTER JOIN ExamDetails
ON SubjectMaster.subject_id = ExamDetails.Subject_id
Any help regarding this would be appreciable
Have you tried this one.
select s.Student_id,e.Subject_id,sub.Subject_id
from YourDatabaseName.SubjectMaster as sub
left outer join YourDatabaseName.ExamDetails as e
on sub.Subject_id=e.Subject_id
left outer join YourDatabaseName.StudentMaster as s
on s.Student_id=e.Student_id
group by s.Student_id
order by s.Student_id;

Using the assigned name of a field with 'AS' within a JOIN

I have this code..
SELECT u.*, COALESCE(c2.name,u.state) AS real_state, c.name AS real_country, bc.alpha3 AS country_iso FROM gimko_v2.users AS u
INNER JOIN gimko_v2.countries AS c ON u.country=c.id
LEFT JOIN gimko_v2.countries AS c2 ON u.state=c2.id
LEFT JOIN gimko_blesta.countries AS bc ON c.country_iso_code=bc.alpha2
LEFT JOIN gimko_blesta.states AS bs ON bc.alpha2=bs.country_alpha2 AND real_state=bs.name
WHERE u.blesta_id=0;
The problem likes in this area:
COALESCE(c2.name,u.state) AS real_state
and
LEFT JOIN gimko_blesta.states AS bs ON bc.alpha2=bs.country_alpha2 AND real_state=bs.name
I am getting this error:
Error Code: 1054
Unknown column 'real_state' in 'on clause'
How can I use the result of COALESCE(c2.name,u.state) in a JOIN?
Thanks!
Does gimko_blesta.states even need to be in the source tables? None of its columns are in the select list/criteria, and it's joined in with a LEFT JOIN, so doesn't seem like it's necessary.
In any case, you would need to make it a derived table first, and join gimko_blesta.states to the derived table, or just use the expression in the JOIN, like COALESCE(c2.name,u.state) = bs.name, below:
SELECT u.*, COALESCE(c2.name,u.state) AS real_state, c.name AS real_country, bc.alpha3 AS country_iso
FROM gimko_v2.users AS u
INNER JOIN gimko_v2.countries AS c ON u.country=c.id
LEFT JOIN gimko_v2.countries AS c2 ON u.state=c2.id
LEFT JOIN gimko_blesta.countries AS bc ON c.country_iso_code=bc.alpha2
LEFT JOIN gimko_blesta.states AS bs ON bc.alpha2=bs.country_alpha2 AND COALESCE(c2.name,u.state) = bs.name
WHERE u.blesta_id=0;
You can't use virtual aliases at the same level of your query you may have to repeat the whole expression or use a sub select to access in your parent query
SELECT * FROM (
SELECT u.*,
COALESCE(c2.name,u.state) AS real_state,
c.name AS real_country,
bc.alpha3 AS country_iso ,
bc.alpha2
FROM gimko_v2.users AS u
INNER JOIN gimko_v2.countries AS c ON u.country=c.id
LEFT JOIN gimko_v2.countries AS c2 ON u.state=c2.id
LEFT JOIN gimko_blesta.countries AS bc ON c.country_iso_code=bc.alpha2
) t
LEFT JOIN gimko_blesta.states AS bs ON t.alpha2=bs.country_alpha2 AND t.real_state=bs.name
WHERE t.blesta_id=0;
Also in sub-select you have to select all the needed columns that will be used in outer join for parent query like in your case i have selected bc.alpha2 in inner query because to join with states this column is needed