So I have checked all the names etc are correct, the query works with only one of the joins not both with a syntax error:
SELECT *, Invoice_Accounts.Title AS t1, Invoice_Accounts.Forename AS f1, Invoice_Accounts.Surname AS s1
FROM Invoice_Accounts
RIGHT JOIN Delivery_Points ON Invoice_Accounts.Account_No = Delivery_Points.Account_No
RIGHT JOIN Companies ON Invoice_Accounts.account_id = Companies.Company_No
This is the query I want to run but it comes up with the error:
syntax error (missing operator)
UPDATE:
using:
SELECT *,
Invoice_Accounts.Title AS t1,
Invoice_Accounts.Forename AS f1,
Invoice_Accounts.Surname AS s1
FROM (Invoice_Accounts
RIGHT JOIN Delivery_Points
ON Invoice_Accounts.Account_No = Delivery_Points.Account_No)
RIGHT JOIN Companies
ON Invoice_Accounts.account_id = Companies.Company_No
Gets me the error "JOIN expression not supported"
In Access you need to use parenthesis around joins if you have more than one:
SELECT *,
Invoice_Accounts.Title AS t1,
Invoice_Accounts.Forename AS f1,
Invoice_Accounts.Surname AS s1
FROM (Invoice_Accounts
RIGHT JOIN Delivery_Points
ON Invoice_Accounts.Account_No = Delivery_Points.Account_No)
RIGHT JOIN Companies
ON Invoice_Accounts.account_id = Companies.Company_No
Two extend this, if you had three joins, you would need another set of parentheses:
SELECT *
FROM (( A
INNER JOIN B
ON B.AID = A.AID)
INNER JOIN C
ON C.BID = B.BID)
INNER JOIN D
ON D.CID = C.CID;
EDIT
I did not know that you could not RIGHT JOIN to the same table twice, so the above is an error, having said that I have never used a RIGHT JOIN in production code in my life, I would be inclined to switch to the more widely used LEFT JOIN and change the order of the tables:
SELECT *,
ia.Title AS t1,
ia.Forename AS f1,
ia.Surname AS s1
FROM (Companies AS c
LEFT JOIN Invoice_Accounts AS ia
ON ia.account_id = c.Company_No)
LEFT JOIN Delivery_Points AS dp
ON ia.Account_No = dp.Account_No;
N.B. I have used aliases to reduce the amount of text in the query, this (in my opinion) makes them easier to read
Figured out the answer eventually:
SELECT Invoice_Accounts.Title AS t1, Invoice_Accounts.Forename AS f1, Invoice_Accounts.Surname AS s1, *
FROM (Companies
RIGHT JOIN Invoice_Accounts ON Companies.Company_No = Invoice_Accounts.Company_Name
)
RIGHT JOIN Delivery_Points ON Invoice_Accounts.Account_No = Delivery_Points.Account_No;
The issue arises from ancient ms-access technologies where you can't right join to the same table more than once!
Related
So I was working on the problem of optimizing the following query I have already optimized this to the fullest from my side can this be further optimized?
select distinct name ad_type
from dim_ad_type x where exists ( select 1
from sum_adserver_dimensions sum
left join dim_ad_tag_map on dim_ad_tag_map.id=sum.ad_tag_map_id and dim_ad_tag_map.client_id=sum.client_id
left join dim_site on dim_site.id = dim_ad_tag_map.site_id
left join dim_geo on dim_geo.id = sum.geo_id
left join dim_region on dim_region.id=dim_geo.region_id
left join dim_device_category on dim_device_category.id=sum.device_category_id
left join dim_ad_unit on dim_ad_unit.id=dim_ad_tag_map.ad_unit_id
left join dim_monetization_channel on dim_monetization_channel.id=dim_ad_tag_map.monetization_channel_id
left join dim_os on dim_os.id = sum.os_id
left join dim_ad_type on dim_ad_type.id = dim_ad_tag_map.ad_type_id
left join dim_integration_type on dim_integration_type.id = dim_ad_tag_map.integration_type_id
where sum.client_id = 50
and dim_ad_type.id=x.id
)
order by 1
Your query although joined ok, is an overall bloat. You are using the dim_ad_type table on the outside, just to make sure it exists on the inside as well. You have all those left-joins that have NO bearing on the final outcome, why are they even there. I would simplify by reversing the logic. By tracing your INNER query for the same dim_ad_type table, I find the following is the direct line. sum -> dim_ad_tag_map -> dim_ad_type. Just run that.
select distinct
dat.name Ad_Type
from
sum_adserver_dimensions sum
join dim_ad_tag_map tm
on sum.ad_tag_map_id = tm.id
and sum.client_id = tm.client_id
join dim_ad_type dat
on tm.ad_type_id = dat.id
where
sum.client_id = 50
order by
1
Your query was running ALL dim_ad_types, then finding all the sums just to find those that matched. Run it direct starting with the one client, then direct with JOINs.
I have joined 1 table twice on the same query, I keep getting error messages that the 'FROM clause have same exposed names. Even using AS does not seem to work, any ideas or suggestions?
here is the query I am using;
select Contact.*, PERSON.*, address.*
from address
full join Contact
on address.uprn = Contact.uprn
full join PERSON
on Contact.contactno = PERSON.contact
full join address
on address.uprn = PERSON.driveruprn
select Contact.*, PERSON.*, a1.*, a2.*
from address a1
full join Contact
on a1.uprn = Contact.uprn
full join PERSON
on Contact.contactno = PERSON.contact
full join address a2
on a2.uprn = PERSON.driveruprn
, however there is no full join in mysql, workaround
select * from t1
left join t2 ON t1.id = t2.id
union
select * from t1
right join t2 ON t1.id = t2.id
You have to alias the second and subsequent usages of a table:
select ...
from address <---first usage
join contact ...
join person ...
join address AS other_address ... <---second usage
^^^^^^^^^^^^^^^^
Doesn't really matter exactly where you do the aliases, but if you use a single table multiple times, all but ONE of those usages have to have unique aliases.
This is probably because you have same field name in different table
change it like this to make sure fieldnames are unique
SELECT
Contact.field1 as c_field1, Contact.field2 as c_field2 ...,
PERSON.field1 as p_field1, PERSON.field2 as p_field2 ...,
address.field1 as a_field1, address.field2 as a_field2 ...
You need to use a separate alias on each of the address table references in your query to avoid the error you are seeing:
SELECT Contact.*, PERSON.*, a1.*, a2.*
FROM address a1 INNER JOIN Contact ON a1.uprn = Contact.uprn
INNER JOIN PERSON ON Contact.contactno = PERSON.contact
INNER JOIN address a2 ON a2.uprn = PERSON.driveruprn
By the way, there is no FULL JOIN in MySQL, so I have replaced them with INNER JOIN which is likely what you had in mind.
I've been trying to write some code in SQL, but it keeps coming up with a syntax error regarding the join, and I can't work out why.
SELECT `COUNTRY$`.country_name, `PARTNER$`.partner_name, count(member_id)
FROM `Member$`
Left Join `COUNTRY$`
ON `MEMBER$`.country_id=`COUNTRY$`.country_id
lEFT jOIN `PARTNER$`
on `MEMBER$`.partner_ID = `PARTNER$`.partner_ID
Group By country_name,Partner_name
Any help would be appreciated.
May have something to do with how your table names are in 'thisFormat$'. Also you did not specify which table member_id was coming and group by also doesn't specify which table country_name, partner_name was from.
Try putting aliases on the tablenames and see if that eliminates the problem
SELECT c.country_name, p.partner_name, count(m.member_id)
FROM `Member$` m
left join `COUNTRY$` c on c.country_id = m.country_id
left join `PARTNER$` p on p.partner_id = m.partner_id
GROUP BY c.country_name, p.partner_name
My query is running too slow.....
any ways to speed it up?
SELECT SQL_CALC_FOUND_ROWS a.*, b.*, c.*, d.*, e.*, f1.*, st6.*
FROM tbl_a a
LEFT JOIN tbl_b b ON a.m_id = b.m_id
LEFT JOIN tbl_c c ON a.ms_id = c.ms_id
LEFT JOIN tbl_d d ON a.gd_id = d.gd_id
LEFT JOIN tbl_e e ON a.sd_id = e.sd_id
LEFT JOIN tbl_f f1 ON a.tp_id = f1.tp_id
LEFT JOIN
(
SELECT g.*, GROUP_CONCAT(f2.tp_name SEPARATOR ',') tp_name, f2.tp AS tp
FROM tbl_g g
LEFT JOIN tbl_f f2 ON g.tp_id = f2.tp_id
GROUP BY s_id
)st6 ON st6.s_id = a.s_id
Do not use LEFT unless you have a specific reason for it. I see no hint of such here.
A query like this is best optimized if it can perform the subquery first. However, 'LEFT' is a hint that it cannot be. Remove all the LEFTs and see if it is fast enough and gives the "right" answer.
Check to see that there is a PRIMARY KEY or INDEX on gd_id in both of the indicated tables in the following. (And do likewise for other JOINs.)
JOIN tbl_d d ON a.gd_id = d.gd_id
If you still need help, provide SHOW CREATE TABLE for each table. And EXPLAIN SELECT ...;.
I have two queries that result two result sets i need to compare both the result sets and need to display the differences between them.Hope i will get good support.Thank you.These are my queries
Query:1
SELECT distinct c.sid_ident,c.fix_ident from corept.std_sid_leg as c INNER JOIN (SELECT sid_ident, transition_ident, max(sequence_num) seq, route_type FROM corept.std_sid_leg WHERE data_supplier='J' AND airport_ident='KBOS' GROUP BY sid_ident,transition_ident) b ON c.sequence_num=b.seq and c.sid_ident = b.sid_ident and c.transition_ident =b.transition_ident WHERE c.data_supplier='J' and c.airport_ident='KBOS';
Query:2
SELECT name,trans FROM skyplan_deploy.deploy_sids ON d.name=c.sid_ident WHERE apt = 'KBOS' AND name != trans;
Comparison is to be done on fields sid_ident in corept.std_sid_leg and name in skplan_deplay.deploy_sids. As Mysql does not support full outer join,I thought of using left join and right join and combine both the results.But i stuck up with this.Please help.I am getting syntax error while using left and right join.Thank you.
The following query should simulate a FULL OUTER JOIN in MySQL.
SELECT *
FROM A
LEFT OUTER JOIN B
ON A.NAME = B.NAME
WHERE B.ID IS NULL
UNION ALL
SELECT *
FROM B
LEFT OUTER JOIN A
ON B.NAME = A.NAME
WHERE A.ID IS NULL;
Compare the results of the with an actual FULL OUTER JOIN in SQL Server and you'll see it works.