MS Access Multi-Join Query - mysql

Can anyone find what is wrong with this MS Access Query? When I try to execute it i receive an error about a missing Operator before the 2nd Left Join
SELECT * FROM (
SELECT GetitUsageTemp.MemberID,
GetitUsageTemp.IDNumber,
GetitUsageTemp.Title,
GetitUsageTemp.Initials,
GetitUsageTemp.Forenames,
GetitUsageTemp.Surnames,
GetitUsageTemp.CellNumber,
GetitUsageTemp.EmailAddress,
Nz(August.[AugustUsage],0) AS AugustUsage
FROM GetitUsageTemp
LEFT JOIN
(SELECT dbo_Requests.fk_Members_ID, Count(dbo_Requests.Log_date) AS JulyUsage
FROM dbo_Requests
WHERE dbo_Requests.Log_date Between #07/01/2013# And #08/01/2013#
GROUP BY dbo_Requests.fk_Members_ID
) Requests
ON GetitUsageTemp.MemberID = Requests.fk_Members_ID
LEFT JOIN
(SELECT dbo_Requests.fk_Members_ID, Count(dbo_Requests.Log_date) AS AugustUsage
FROM dbo_Requests
WHERE dbo_Requests.Log_date Between #08/01/2013# And #09/01/2013#
GROUP BY dbo_Requests.fk_Members_ID
) August
ON GetitUsageTemp.MemberID = August.fk_Members_ID
)GETIT

In Access you can only join two tables. If you need to join more tables, you need to group the first join together using parentheses, as if it was a new derived table. Then you can join another table to that group:
select
*
from
( ( Table1
LEFT JOIN Table2 ...
)
LEFT JOIN Table3 ...
)
LEFT JOIN Table4 ...
(I'm using awkward indentation to try to make the groups more clear)

Related

SQL to compare records with and without given string is very slow, IS it possible to optimize?

I have this query.
SELECT WithoutX.Field1, WithoutX.Field3 ,AVG(WithoutX.Field2) AS WO_X, AVG(withX.Field2) AS with_X
FROM (
SELECT ROUND(Table1.Field3,1) as Field3,Table3.Field2,Table2.Field1
FROM Table1
INNER join Table2
ON Table1.Field5 = Table2.Field5 AND instr (Table2.Field1,'X')>0
LEFT JOIN Table3 ON Table3.Field4 = Table1.Field4
) AS withX
INNER JOIN (
SELECT ROUND(Table1.Field3,1) as Field3,Table3.Field2,Table2.Field1
FROM Table1
INNER join Table2
ON Table1.Field5 = Table2.Field5 AND instr (Table2.Field1,'X')=0
LEFT JOIN Table3 ON Table3.Field4 = Table1.Field4
) AS WithoutX ON withX.Field3 = WithoutX.Field3 AND removeX(withX.Field1,'X') = WithoutX.Field1
GROUP BY WithoutX.Field1, WithoutX.Field3
removeX is a function that removes the substring x and other parts of the string in field1, so that the two derived tables WithoutX and WithX can be join based on field1.
It's working but it's very slow. I checked each parts separately and it seams the slowing down comes from the main INNER Join and the Group By. But the function does not change much the speed. Is there any way to optimize this query ?

Mysql query join table and let fill with empty if value same

I Have problem with joining 2 table
this is mysql query
select *
from tbl_perspective a
inner join tbl_objective b on b.idperspective=a.idperspective
The result is:
Query Result
I Want to display first row of perspectivename and blank or null
Final Result:
enter image description here
Hi Anwr Rawk simply you can use LEFT JOIN
select * from tbl_perspective as a
left join
tbl_objective as b
on b.idperspective=a.idperspective
Join to a subquery which identifies the first row for each idperspective group:
SELECT t1.*
FROM tbl_perspective t1
INNER JOIN
(
SELECT idperspective, MIN(idobjective) AS min_idobjective
FROM tbl_perspective
GROUP BY idperspective
) t2
ON t1.idperspective = t2.idperspective AND
t1.idobjective = t2.min_idobjective;
You need to use LEFT JOIN to include all results, like this : https://www.codeproject.com/Articles/33052/Visual-Representation-of-SQL-Joins

How to access parent column from a subquery within a join

I'm trying to left join the second table useri_ban based on the users' ids, with the extra condition: useri_ban.start_ban = max_start.
In order for me to calculate max_start, I have to run the following subquery:
(SELECT MAX(ub.start_ban) AS max_start, user_id FROM useri_ban ub WHERE ub.user_id = useri.id)
Furthermore, in order to add max_start to every row, I need to inner join this subquery's result into the main result. However, it seems that once I apply that join, the subquery is no longer able to access useri.id.
What am I doing wrong?
SELECT
useri.id as id,
useri.email as email,
useri_ban.warning_type_id as warning_type_id,
useri_ban.type as type,
useri.created_at AS created_at
FROM `useri`
inner join
(SELECT MAX(ub.start_ban) AS max_start, user_id FROM useri_ban ub WHERE ub.user_id = useri.id) `temp`
on `useri`.`id` = `temp`.`user_id`
left join `useri_ban` on `useri_ban`.`user_id` = `useri`.`id` and `useri_ban`.`start_ban` = `max_start`
Does this solve your problem? You need GROUP BY in the inner query instead of another join.
SELECT useri.id, useri.email, maxQuery.maxStartBan
FROM useri
INNER JOIN
(
SELECT useri_ban.user_id ubid, MAX(useri_ban.startban) maxStartBan
FROM useri_ban
GROUP BY useri_ban.user_id
) AS maxQuery
ON maxQuery.ubid = useri.id;

Joining 1 table twice in the same SQL query

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.

Delete and Update Query Using Multiple Tables

I have the following select statement:
select *
from ( select q.*,
qg.index_min as groupIndexMin,
qg.index_max as groupIndexMax,
fo.form_condition,
fo.form_array,fo.form_index
from question_group qg, question q
right join form fo on q.form_id = fo.form_id
where q.form_id=14 and
(q.question_group_id=qg.question_group_id and q.question_type_id NOT IN(12))
order by question_order) m
left join (select r.response_text,
r.response_id,
r.response_index,
f.*,
a.*
from response r
right outer join field f on r.field_id=f.field_id and r.response_index=5
right outer join application a on r.application_id=a.application_id
right outer join user u on a.user_id=u.user_id
where (a.application_id=70 and u.user_id=47)) n on m.field_id = n.field_id
order by m.question_order ;
Now I need,
Delete Query : for the above select query using same conditions the record will be delete where response_index=5.
Update Query : Suppose, if I delete response_index=5, then remaining response_index will be update. For example response_index=6 => response_index=5 and response_index=7 => response_index=6 like that.
Please help me.