Click here to Skip to main content
15,892,059 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
i have to join table below table:
join this 4 table
schd_mst:sch_id,date
schd_detail:sch_id,cust_id
cust_mst:cust_id,cust_name,city_id
city_mst:city_id,city_name

join this 3 table:
tour_mst:date,cust_id
cust_mst:cust_id,cust_name,city_id
city_mst:city_id,city_name


my 2 query as below:

SQL
select sm.date,cm.cust_name,cim.city_name   from schd_detail inner join schd_mst sm on sm.Sch_id=sd.Sch_id inner join cust_mst cm on sd.cust_id =cm.cust_id inner join city_mst cim on cm.city_name=cim.city_id  where sm.date='2013-11-01'

SQL
select tm.date,cm.cust_name,cim.city_name from tour_mst tm inner join cust_mst cm on tm.cust_id =cm.cust_id inner join city_mst cim on cm.city_name=cim.city_id  where tm.date='2013-11-01'


i want to join in single query with above tables
Posted
Updated 8-Dec-13 11:02am
v2
Comments
Peter Leow 8-Dec-13 22:52pm    
Some error in your original sql statements:

1. there is a missing 'sd' after 'from schd_detail' in the first sql statement, and

2 It should be 'cm.city_id = cim.city_id' and not 'cm.city_name=cim.city_id' in both statements.

Hi,

select sm.date,cm.cust_name,cim.city_name from schd_detail inner join schd_mst sm on sm.Sch_id=sd.Sch_id inner join cust_mst cm on sd.cust_id =cm.cust_id inner join city_mst cim on cm.city_name=cim.city_id where sm.date='2013-11-01'

Union ALL

select tm.date,cm.cust_name,cim.city_name from tour_mst tm inner join cust_mst cm on tm.cust_id =cm.cust_id inner join city_mst cim on cm.city_name=cim.city_id where tm.date='2013-11-01'


Check this Once it will Work for u........
 
Share this answer
 
v2
You did not specify what kind of problem do you have...

Have a look here: Visual Representation of SQL Joins[^]
Above article should help you understand how JOIN's works ;)
 
Share this answer
 
Comments
priyanshbhaliya 9-Dec-13 8:49am    
i also use union but it wont work as i need:
below data:
------------------------------------------------
schd_mst:
sch_id date
1 2013-11-01
-------------------------------------------------------
schd_detail:
sch_id cust_id
1 1
1 2
----------------------------------------------------------
cust_mst:
cust_id cust_name,city_id
1 xyz 1
2 zzz 1
3 zz 1
4 qqq 1
5 qq 1
---------------------------------------------
city_mst:
city_id,city_name
1 aaa

-----------------------------------
tour_mst:
date cust_id
2013-11-01 3
2013-11-01 4
2013-11-01 5

city and cust table is from above:
output i need is below
date cust_name city cust_name city
2013-11-01 xyz aaa qqq aaa
2013-11-01 zzz aaa zzz aaa
2013-11-01 zz aaa

first cust_name and city from :: schd_mst,schd_detail,cust_mst,city_mst
other cust_name and city from :: tour_mst,cust_mst,city_mst

this my query:
select sm.date,cm.cust_name ,cim.city_name,'' customer,'' city from Schd_Deatils sd inner join Schd_mst sm on sm.Sch_id=sd.Sch_id inner join Cust_mst cm on sd.cust_id =cm.cust_id inner join Citymst cim on cm.city_name=cim.city_id where sm.date='2013-11-01'
union all
select tm.date,'' MTPcust,'' as MTPCity,cm.cust_name cust_name,cim.city_name city_name from Tour_mst tm inner join Cust_mst cm on tm.cust_id =cm.cust_id inner join City_mst cim on cm.city_name=cim.city_id where tm.date ='2013-11-01'
SQL
SELECT sm.date, cm.cust_name, cim.city_name FROM
schd_detail AS sd INNER JOIN  schd_mst AS sm ON sm.sch_id=sd.sch_id
INNER JOIN cust_mst AS cm ON sd.cust_id =cm.cust_id
INNER JOIN city_mst AS cim ON cm.city_id=cim.city_id where sm.date='2013-11-01'
UNION
SELECT tm.date, cm.cust_name, cim.city_name FROM
tour_mst AS tm INNER JOIN cust_mst AS cm ON tm.cust_id =cm.cust_id
INNER JOIN  city_mst AS cim ON cm.city_id=cim.city_id where tm.date='2013-11-01'
 
Share this answer
 
U can go for CTE(common table expression) as well for that. It's easy to understand...
 
Share this answer
 
<br />
select sm.date,cm.cust_name,cim.city_name   <br />
	from schd_detail inner join schd_mst sm on sm.Sch_id=sd.Sch_id <br />
			inner join cust_mst cm on sd.cust_id =cm.cust_id <br />
			inner join tour_mst tm on tm.cust_id =cm.cust_id<br />
			inner join city_mst cim on cm.city_name=cim.city_id  <br />
where sm.date='2013-11-01'<br />


Hope this will help you.

Thanks,
Bhvaesh
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900