How to join 3 tables by a single sql query? - sql

I have 3 tables.
Table1: Group_Code, Group_Name,companyID;(PK: Group_Code)
Table2: PartyID,GroupID,companyID;(FK: GroupID, PK:PartyID)
Table3: VendorID, companyID;(FK:VendorID)
I want to fetch Group_Name from Table1 for all VendorID of Table3. How can I do this?
here i write a code. But it shows "Syntex error in FROM clause." My database is in ms access.
select Group_Name from Table1 join Table2 on Table1.Group_Code= Table2.GroupID
join Table3 on Table2.PartyID=Table3.VendorID

try this !!!
SELECT table1.group_name FROM (table1
INNER JOIN ON table1.group_code=table2.groupid)
INNER JOIN table3 ON table2.partyid=table3.vendorid
GROUP BY table1.group_name

select Group_Name from Table1
join Table2 on Table1.Group_Code = Table2.GroupID
join Table3 on Table2.PartyID = Table3.VendorID

SELECT table1.group_name FROM table1 join table2
ON table1.group_code=table2.groupid
join table3 ON table2.partyid=table3.vendorid
error becoz you didnt take the group name DB instance ?

You Can do this :
select Table1.Group_Name, Table3.VendorID from Table1 join Table2 on Table1.Group_Code= Table2.GroupID join Table3 on Table2.PartyID =Table3.VendorID
If you are data has been stored in proper relationship. the query should get you going. :)

use this code for question,
Select Table1.Group_Name from ((Table1
left join Table2 on Table1.Group_Code=Table2.GroupID)
left join Table3 on Table2.PartyID=Table3.VendorID)

Related

Inner join the tables where the value of one column is different. But the meaning of the values are same

Table t1
Table t2
I tried running the query
select CountyCode, ContactPerson
from table1 t1
inner join select * from table 2 t2 on t1.CountyCode[1] = Code[0]
Any one help me please.
Use this syntax:
select CountyCode, ContactPerson
from t1
inner join t2 on t1.CountyCode = t2.Code

SQL joining of two different queries

Table1: column is( ID)
Table2: column is (NAME)
QUERY:
SELECT ID FROM TABLE1
SELECT NAME FROM TABLE2
NOW, I want output side by side , is there any possibility
Try this....
if this is without any condition
select id, name from table1, table2
I woder whether you mean CROSS JOIN
select id, name
from table1
cross join table2
You need to do an JOIN between tables on a value that exists in both tables. An INNER JOIN only takes rows that has a match in both tables.
Example:
SELECT tb1.id, tb2.name FROM table1 AS tb1 INNER JOIN table2 AS tb2 ON tb1.id = tb2.table1_id;

Access SQL - Left join, Group by, Count(select where)

I have 2 tables.
tbl1 has colums:
id1, someothercolumns
tbl2 has colums:
id2, id1, bool
I can join them by id1 without problem. I can do this:
SELECT tbl1.id1, Count(tbl2.id2) AS CountOfid2
FROM tbl1 LEFT JOIN tbl2 ON tbl1.id1= tbl2.id1
GROUP BY tbl1.id1;
But I don't want to count all items from tbl2, on those where
bool=false
So, I want to see all records from tbl1 and not count all records from tbl2. I tried with sub-select, but Access doesn't like my ideas. :(
Can you help me?
I figured out the solution:
SELECT tbl1.id1, Count(tmptbl.id2) AS CountOfid2
FROM tbl1 LEFT JOIN (SELECT tbl2.id1, tbl2.id2 FROM tbl2 WHERE bool=false) as tmptbl ON tbl1.id1= tmptbl.id1
GROUP BY tbl1.id1;
Give me cookie!
please test this script:
select distinct t1.id1,t4.countofID2 from tbl1 t1
left outer join
(select t3.con as countofID2 ,* from tbl2
,(SELECT COUNT(t2.id2)as con
FROM tbl2 t2) as t3)as t4
on(t1.id1=t4.id1)
Be happy .....

SQL join order and conditions

I've got 3 tables that I want to join and filter on some conditions.
I've first wrote this query:
select * from table1 t1
left join (select * from table2 where table2.fieldX=...) t2
on t1.id_12=t2.id_12
left join table3
on t2.id_23=t3.id_23
where t1.fieldY=...
Then I wanted to make it looks like more canonical by rewritting it like that:
select * from table1 t1
left join table2 t2
on t1.id_12=t2.id_12
left join table3
on t2.id_23=t3.id_23
where table2.fieldX=...
and t1.fieldY=...
But it does not give the same result.
I dont't understand why...
Do you?
Thanks in advance.
When you put table2.fieldX=... in the where clause you eliminate all rows from table1 that do not have a corresponding row in table2. Effectively you are changing the left join into an inner join.
Instead, you can apply the table2 filter in the join itself:
SELECT
*
FROM
Table1 t1
LEFT JOIN Table2 t2 ON t1.id_12 = t2.id_12 AND t2.fieldX = ...
LEFT JOIN Table3 t3 ON t2.id_23 = t3.id_23
WHERE
t1.fieldY = ...
Did you add the inner select where on the second query?
SELECT *
FROM table1 t1
LEFT JOIN table2 t2
on t1.id_12=t2.id_12
LEFT JOIN table3
on t2.id_23=t3.id_23
WHERE t1.fieldY=...
and t2.fieldX=...

Retrieving data from the table in SQL Server 2008

I have 4 tables
table1 (id, stateId(fk), name, carId(fk))
table2 (stateId(pk), state, countryId(fk))
table3 (countryId, country, currency)
table4 (carId, car)
I want to retrieve name, state, country, car from the above tables through stored procedure using joins. And if some other easy way then please tell.
thanks.
A simple JOIN should do:
CREATE PROCEDURE schema.YourStoredProcedureName
AS
SELECT t1.name, t2.state, t3.country, t4.car
FROM table1 t1
JOIN table2 t2 ON t1.stateId = t2.stateId
JOIN table3 t3 ON t2.countryId = t3.countryId
JOIN table4 t4 ON t1.carId = t4.carId
GO
No other easy method. Join is basic and easy (http://msdn.microsoft.com/en-us/library/ms191517(v=sql.105).aspx)
select tbl1.name, tbl2.state, tbl3.country, tbl4.car
From table1 tbl1
inner join table2 tbl2 on tbl1.stateid =tbl2.stateid
inner join table3 tbl3 on tbl2.countryid = tbl3.countryid
inner join table4 tbl4 on tbl1.carId = tbl4.carId