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)
Related
I am using code:
create table table3 as
select a.*,b.*
from
table1 a
left join
table2 b
on a.id=b.id
where a.date>=b.date
and a.age<b.age
however,table1 has 20000 rows and table3 has only 5000 rows.It seems the where clause filter all the null values.
When I am using code:
create table table4 as
select a.*,b.*
from
table1 a
left join
table2 b
on (a.id=b.id
and a.date>=b.date
and a.age<b.age)
I would get an error "both left and right aliases encountered in join",because I used inequality in join conditions.
So,how could I get more than 20000 rows include null values by using left join?Should I join multiple times or could use another more effective way?
The inner join works, i can left join the second table, but i cant join the third table.
I dont know why, even if i inner join the first table and then left join the third it doenst work.
If i inner join the table and left join with the second it works like said, but when i put the third table into the query it fails again.
create table xxxx AS select
t1.XXX,
t2.YYY,
t3.ZZZ
from
DB_AA..P1 t1
INNER JOIN DB_TT..TQ t5 on (t5.NR = t1.NR)
LEFT JOIN DB_II..GG t2 ON (t2.LS = t1.LS)
LEFT JOIN DB_AA..PP t3 ON (t3.NR= t1.NR)
Anybody see something i dont see?
I pass the code to a Netezza.
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)
Can sombody Explains me about joins?
Inner join selects common data based on where condition.
Left outer join selects all data from left irrespective of common but takes common data from right table and vice versa for Right outer.
I know the basics but question stays when it comes to join for than 5, 8, 10 tables.
Suppose I have 10 tables to join. If I have inner join with the first 5 tables and now try to apply a left join with the 6th table, now how the query will work?
I mean to say now the result set of first 5 tables will be taken as left table and the 6th one will be considerded as Right table? Or only Fifth table will be considered as left and 6th as right? Please help me regarding this.
When joining multiple tables the output of each join logically forms a virtual table that goes into the next join.
So in the example in your question the composite result of joining the first 5 tables would be treated as the left hand table.
See Itzik Ben-Gan's Logical Query Processing Poster for more about this.
The virtual tables involved in the joins can be controlled by positioning the ON clause. For example
SELECT *
FROM T1
INNER JOIN T2
ON T2.C = T1.C
INNER JOIN T3
LEFT JOIN T4
ON T4.C = T3.C
ON T3.C = T2.C
is equivalent to (T1 Inner Join T2) Inner Join (T3 Left Join T4)
It's helpful to think of JOIN's in sequence, so the former is correct.
SELECT *
FROM a
INNER JOIN b ON b.a = a.id
INNER JOIN c ON c.b = b.id
LEFT JOIN d ON d.c = c.id
LEFT JOIN e ON e.d = d.id
Would be all the fields from a and b and c where all the ON criteria match, plus the values from d where its criteria match plus all the contents of e where all its criteria match.
I know RIGHT JOIN is perfectly acceptable, but I've found in my experience that it's unnecessary - I almost always just join things from left to right.
> Simple INNER JOIN VIEW code...
CREATE VIEW room_view
AS SELECT a.*,b.*
FROM j4_booking a INNER JOIN j4_scheduling b
on a.room_id = b.room_id;
You can apply join like this..
select a.*,b.*,c.*,d.*,e.*
from [DatabaseName].[Table_a] a
INNER JOIN [DatabaseName].[Table_b] b ON a.id = b.id
INNER JOIN [DatabaseName].[Table_c] c ON b.id=c.id
INNER JOIN [DatabaseName].[Table_d] d on c.id=d.id
INNER JOIN [DatabaseName].[Table_e] e on d.id=e.id where a.con=5 and
b.con=6
Here, at place of a.* and in where condition, you can show column(filed) which you like and according condition in where condition. You can insert more table and database as per your choice. But mind that you need to mention database name and alias if you work in different database.
Just tried the following from the Example DataBase given in W3School. Worked Fine for me.
SELECT Orders.OrderID, Customers.CustomerName, Orders.OrderDate, Products.ProductName, Products.ProductID
FROM Orders
INNER JOIN Products
INNER JOIN Customers
ON Orders.CustomerID=Customers.CustomerID;
Join used to combine rows from two or more tables, based on a related column between them. This example from Adventure works:
SELECT a.[EmailAddress],b.[FirstName],b.[LastName],c.[PhoneNumber],d.[Name]
FROM [Person].[EmailAddress] a
INNER JOIN [Person].[Person] b
ON a.BusinessEntityID = b.BusinessEntityID
INNER JOIN [Person].[PersonPhone] c
ON b.BusinessEntityID = c.BusinessEntityID
INNER JOIN [Person].[PhoneNumberType] d
ON c.phoneNumberTypeID = d.phoneNumberTypeID
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.