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
Related
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.
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.
When i run Following Query everything works well but the subQuery inside LEFT JOIN Fails and
SELECT inv.*,rc.*,po.*,cfd.*
FROM invoice_lineitem as inv
LEFT JOIN (
SELECT rc1.* FROM rentalcode as rc1 WHERE rc1.assetCategory=
(SELECT AssetCategory FROM assetmaster WHERE
AssetID=invoice_lineitem.asset_typeID)
AND invoice_lineitem.invoice_date between rc1.start_date
AND rc1.validity_date
) rc ON rc.email=inv.usernameID
LEFT OUTER JOIN purchase_order as po ON (po.PO_ID=inv.poNumber)
LEFT OUTER JOIN client_facility_data as cfd ON (cfd.email=inv.usernameID)
WHERE inv.contractID='2/1AS/10'
I get Error as
1054 - Unknown column 'invoice_lineitem.asset_typeID' in 'where
clause'
When i try the same query by just removing the all invoice_lineitem from SubQuery and placing it in WHERE(beside contractID) the query works but it acts like INNER JOIN Query i.e if Query Fails for any of the Where Clause i get No Data
SELECT inv.*,rc.*,po.*,cfd.*
FROM invoice_lineitem as inv
LEFT JOIN (
SELECT rc1.* FROM rentalcode as rc1
) rc ON rc.email=inv.usernameID
LEFT OUTER JOIN purchase_order as po ON (po.PO_ID=inv.poNumber)
LEFT OUTER JOIN client_facility_data as cfd ON (cfd.email=inv.usernameID)
WHERE inv.contractID='2/1AS/10' AND rc.assetCategory=(SELECT AssetCategory FROM assetmaster WHERE AssetID=inv.asset_typeID)
AND inv.invoice_date between rc.start_date and rc.validity_date
A subquery can't refer to tables in the outer query (and if it could, you would have to use the inv. alias).
Try this. I changed the subquery into another LEFT JOIN.
SELECT inv.*,rc.*,po.*,cfd.*
FROM invoice_lineitem as inv
LEFT JOIN rentalcode AS rc1
ON rc.email = inv.usernameID
AND inv.nvoice_date between rc1.start_date AND rc1.validity_date
LEFT JOIN assetmaster AS am
ON rc1.assetCategory = am.AssetCategory
AND inv.asset_typeID = am.AssetID
LEFT OUTER JOIN purchase_order as po ON (po.PO_ID=inv.poNumber)
LEFT OUTER JOIN client_facility_data as cfd ON (cfd.email=inv.usernameID)
WHERE inv.contractID='2/1AS/10'
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
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