sql access inner join of 4 tables - sql

I am new to sql and I am trying to join 4 tables together but just cant get the hang of it.
I am trying to do this with an inner join but I always get an syntax error with access.
SELECT *
from kdst,aufpos
inner join( artst inner join vert on kdst.vertreter = vert.vertnr)
on aufpos.artnr = artst.artnr;
This is my code but it does not work. I dont know what to do anymore, I hope someone can help me.

Build using the query design window
Then switch to sql view

Select *
From table1 t1
Inner join table2 t2 on t1.id = t2.fkid
Inner join table3 t3 on t1.id = t3.fkid
...
This is if you want to join multiple tables to the same parent table (table1). Fkid is the column of the foreign key field that refers to the primary key of the parent table.

Related

SQL - how to rename column being used on inner join

I have created a view in Microsoft SQL Server Management Studio, and I now want to join it to the correct spatial information.
I have created the query below
select * from v_postal_address_view pa
INNER JOIN SPATIAL_INFO sp ON sp.ECAD_ID = pa.BUILDING_ID
This runs fine as the ECAD_ID and BUILDING_ID are linked. However, I now want to save my results as a new view but cannot because the v_postal_address_view contains ECAD_ID already, therefore forbidding the SPATIAL_INFO tables ECAD_ID to be joined as the name is not unique.
How can I rename the ECAD_ID column in spatial_info during the inner join to the view??
CREATE VIEW RESOLVE_AMBIGUOUS_NAMES AS
SELECT T1.id AS id_1,
T2.id AS id_2
FROM TABLE_1 T1
INNER
JOIN TABLE_2 T2
ON T1.fk = T2.pk

Appending and Mapping of two tables in SQL

I have two tables in the database, I want to merge and map the table 2
into table 1
by writing SQL query.
I don't know whether to apply joins or what? and then how to append the data.
note: Customer id is the common key in both tables.
A simple INNER JOIN would solve your problem --
SELECT T1.[Branch Code]
,T1.[CustomerID]
,T2.[Customer Name]
,T1.[Advised Amount]
FROM Table1 T1
INNER JOIN Table2 T2
ON T1.CustomerID = T2.CustomerID
Use Inner join..... U can write column name in place of *
Select * from Table1 as a
inner join Table2 as b on a.CustomerId = b.CustomerId

MS Access SQL Query - Error in Join

i am new to SQL and have been trying to have an SQL query in MS Access to join multiple tables using the below sql. This is for doing access testing.
Have explained what i intent to achieve in bold
SELECT
Table1.Role,
Table1.Object,
Table 1 is the base table which has role, authorization object
Table2.Role,
Table2.User_Name,
Table 2 has the users mapped to the role
Table3.Org_Level_Desp,
Table3.Org_LEvel_Values_1,
Table 3 has controls at organization level, this determine which company or plant the user can access
Table4.Role_Description,
Table 4 has role descriptions for roles mentioned in Table 1
Table5.Full_Name,
Table5.Department,
Table 5 has user name and department. Common field is user name from table 2
FROM
Table1
RIGHT JOIN Table2 ON Table1.Role=Table2.Role
RIGHT JOIN Table3 ON Table1.Role=Table3.Role
RIGHT JOIN Table4 ON Table1.Role=Table4.rOLE
Joining required tabled from Table 2, 3 and 4 to Table 1
FROM
Table2
RIGHT JOIN Table5 ON Table2.USER_NAME=Table5.USer
Joining required tables from table 5 to table 1
I am getting multiple syntax error for the above query. I think i am missing on something basic - Can anyone help ?
Thanks !
Uday
Seems i cannot add more in comment. Sorry, below is the code that i updated, still getting the same error. Can you please advise ?
SELECT
TABLE1.Role,
TABLE1.Object,
TABLE1.Field_name,
TABLE1.Value,
TABLE1.[and],
TABLE1.ID_whether_object_is_deleted,
TABLE2.Role,
TABLE2.User_Name,
TABLE2.End_date,
TABLE3.Org_Level_Control,
TABLE3.Org_LEvel_Values_1,
TABLE3.Org_LEvel_Values_2,
Table4.Role_Description,
TABLE6.Field_Short_Description,
TABLE7.Object_Level_Desp,
TABLE8.Auth_Obj_Text,
TABLE5.Full_Name,
TABLE5.Department,
TABLE9.Valid_to,
TABLE9.Lock,
from ((((((TABLE1 INNER JOIN TABLE2 ON TABLE1.Role=TABLE2.Role)
INNER JOIN TABLE3 ON TABLE1.Role=TABLE3.Role)
INNER JOIN Table4 ON TABLE1.Role=Table4.role)
INNER JOIN TABLE6 ON TABLE1.Field_Name=TABLE6.Field_Name)
INNER JOIN TABLE7 ON TABLE1.[Object]=TABLE7.Org_Object)
INNER JOIN TABLE8 ON AGR_TABLE1.Field_name=TABLE8.Field_Name)
FROM
(TABLE2 INNER JOIN TABLE5 ON TABLE2.USER_NAME=TABLE5.[USer])
INNER JOIN TABLE9 ON TABLE2.USER_NAME=TABLE9.[User]);
Below is the code that i updated, still getting the same error. Can you please advise ?
SELECT
TABLE1.Role,
TABLE1.Object,
TABLE1.Field_name,
TABLE1.Value,
TABLE1.[and],
TABLE1.ID_whether_object_is_deleted,
TABLE2.Role,
TABLE2.User_Name,
TABLE2.End_date,
TABLE3.Org_Level_Control,
TABLE3.Org_LEvel_Values_1,
TABLE3.Org_LEvel_Values_2,
Table4.Role_Description,
TABLE6.Field_Short_Description,
TABLE7.Object_Level_Desp,
TABLE8.Auth_Obj_Text,
TABLE5.Full_Name,
TABLE5.Department,
TABLE9.Valid_to,
TABLE9.Lock,
from ((((((TABLE1 INNER JOIN TABLE2 ON TABLE1.Role=TABLE2.Role)
INNER JOIN TABLE3 ON TABLE1.Role=TABLE3.Role)
INNER JOIN Table4 ON TABLE1.Role=Table4.role)
INNER JOIN TABLE6 ON TABLE1.Field_Name=TABLE6.Field_Name)
INNER JOIN TABLE7 ON TABLE1.[Object]=TABLE7.Org_Object)
INNER JOIN TABLE8 ON AGR_TABLE1.Field_name=TABLE8.Field_Name)
I have no idea why you would want RIGHT JOIN, particularly if you are learning SQL. Start with INNER JOIN. If that is leaving out rows, then move to LEFT JOIN.
The syntax in MS Access uses parentheses:
SELECT . . .
FROM (((Table1 INNER JOIN
Table2
ON Table1.Role = Table2.Role
) INNER JOIN
Table3
ON Table1.Role = Table3.Role
) INNER JOIN
Table4
ON Table1.Role = Table4.rOLE
) INNER JOIN
Table5
ON Table2.USER_NAME = Table5.USer
You can then select the columns you want in the SELECT.
Note that the parentheses would look quite awkward in any other database.
Re: your updated SQL.
Value is a reserved word in Access SQL, so may be others like Role, Object, Lock. Put these into [square brackets], both in the SELECT and the FROM clause.
And you have an extraneous comma at the end of your SELECT clause:
TABLE9.Valid_to,
TABLE9.Lock, <== delete this comma
from ((((((TABLE1 INNER JOIN TABLE2 ON TABLE1.Role=TABLE2.Role)

How to select records that do not exist in two (or more) tables

I have 3 tables of accounts that all contain the same fields. Table1 contains all accounts while Table2 and Table3 contain subsets of the accounts. I'm trying to select records in Table1 that do no exist in Table2 or Table3.
Let's say the table layout is like this and is the same for all 3 tables:
|AcctNum|Name|State|
I know how to do this if it was just Table1 and Table2, using a left join and Is Null, but the 3rd table is throwing me. Is this possible to do in one query? Can you combine left joins? I should point out I'm using Access 2010.
Yes you can combine left joins and with the odd syntax Access uses the query should look like this:
SELECT T1.AcctNum
FROM (Table1 AS T1 LEFT JOIN Table2 AS T2 ON T1.AcctNum = T2.AcctNum)
LEFT JOIN Table3 AS T3 ON T1.AcctNum = T3.AcctNum
WHERE (((T2.AcctNum) Is Null) AND ((T3.AcctNum) Is Null));
You can use Access to create a view called TableCombined that is a union of both Table2 and Table3.
At that point, you can use your left join and Is Null query and join TableCombined to Table1.
Hope this helps!
You can also do a NOT EXISTS statement which makes sense logically for what you are trying to achieve.
For example:
SELECT ACCTNUM
FROM TABLE1
WHERE NOT EXISTS (SELECT TABLE2.ACCTNUM FROM TABLE2 INNER JOIN TABLE3 WHERE TABLE2.ACCTNUM IS NULL AND TABLE3.ACCTNUM IS NULL)

MS Access Inner Join On 3 Tables with the same field_name

I'm doing an assignment for class, and I'm at my whits end. Basically, I need to write a query that uses INNER JOIN's to combine all the data in 4 tables, while avoiding having titles columns with identical names (Ie Table1.Master_Number and Table2.Master_Number). The literal instructions are as follows:
Step 1: Create a view of all the columns (only list the columns once if the columns are duplicated in the tables) in all the tables of the Lecture 5 database. Save this query as Lecture_5_View and us this query to build the following 3 queries. In the “FROM” clause, you MUST use an “INNER JOIN” in this query. Remember ACCESS does not support the “CREATE VIEW” command so you must create a select query. `
Here is a screenshot of the database, that shows all the headings and column headings.
Based on other posts like this, I thought that it would be pretty simple. I have this so far, but it does not want to run
(ie Syntax error(missing operator) in query expression 'Table1.Master_Number = Table2.Master_Number INNER JOIN Table4 ON Table1.Master_Number = Table4.Master_Number)
SELECT *
FROM Table1
INNER JOIN Table2 ON Table1.Master_Number = Table2.Master_Number
INNER JOIN Table4 ON Table1.Master_Number = Table4.Master_Number
Where am I going wrong so far and how to I accomplish this query?
Much thanks,
Josh
With Access you need to use parentheses when doing more than one join:
SELECT *
FROM (Table1
INNER JOIN Table2
ON Table1.Master_Number = Table2.Master_Number)
INNER JOIN Table4
ON Table1.Master_Number = Table4.Master_Number;
This is essentiall spliting down you query so you have at most one join per section, i.e. Your First query is:
SELECT *
FROM Table1
INNER JOIN Table2
ON Table1.Master_Number = Table2.Master_Number;
Then you have:
SELECT *
FROM YourFirstQuery
INNER JOIN Table4
ON Table1.Master_Number = Table4.Master_Number;
This differs slightly from subqueries as you are still able to reference all fields from Table1 and Table2.
EDIT
To avoid duplicating columns you will need to explicitly list the columns you want (although you should be doing this anyway):
SELECT Table1.Master_Number,
Table1.Asset_Tag,
Table1.Serial_Number,
Table2.Last_Name,
Table2.First_Name,
Table4.Office_Number,
Table4.Location,
Table4.Department
FROM (Table1
INNER JOIN Table2
ON Table1.Master_Number = Table2.Master_Number)
INNER JOIN Table4
ON Table1.Master_Number = Table4.Master_Number;
Try this:
SELECT * FROM ((Table1
INNER JOIN Table2
ON Table1.Master_Number = Table2.Master_Number)
INNER JOIN Table4
ON Table1.Master_Number = Table4.Master_Number)