Sanjay
Sanjay

Reputation: 13

how to combine data from two table?

See I have two tables like

Table1:TheaterDetail

t_id    t_Address    t_Name   c_id
1       ahmedabad    Cinemax1   1
2       Baroda       Cinemax2   2
3       Jamnagar     Cinemax3   3
4       Rajkot       Cinemax4   4
5       Surat        Cinemax5   5
6       Junagadh     Cinemax2   2

Table2:CityDetails

c_id    City_name
1       Ahm
2       Bar
3       Jam
4       Raj
5       Sur

I want Out Put Like That is it possible? I am new to sql so i found its hard. I want column name to be display in output (t_address,t_name)

Ahm 
t_address   t_name
ahmedabad   Cinemax1
Bar 
t_address   t_name
Baorda  Cinemax2
Junagadh Cinemax2
Jam 
t_address   t_name
Jamnagar    Cinemax3
Raj 
t_address   t_name
Rajkot  Cinemax4
Sur 
t_address   t_name
Surat   Cinemax5

I do Two query Like These

 select distinct city_name from CityDetail A inner join TheaterDetail B

    on A.c_id = B.c_id where a.c_id= 2

   city_name
   Bar

And Other Query Like this

select t_Address,t_Name from TheaterDetail C inner JOin CityDetail D
on C.c_id = D.c_id where D.c_id= 2

t_Address   t_Name
Baroda      Cinemax2
Junagadh    Cinemax2

My desire Out put will be like this if i put condition on city_id=2

Bar 
t_address   t_name
Baorda      Cinemax2
Junagadh    Cinemax2

Upvotes: 1

Views: 41

Answers (1)

Tarang
Tarang

Reputation: 66

this will be the query you want

    drop table Tbl1
    Create table Tbl1
    (
        iSrno int identity(1,1) not null,
        c_id numeric(18,2),
        city_name nvarchar(500) 
        ,t_name nvarchar(500)
    )

    Insert into Tbl1 
    select c_id,city_name as city_name, '' as t_name from CityDetails
    Union
    select c_id, 't_address','t_name'  from CityDetails

    Insert into Tbl1 
    select b.c_id,a.t_address,a.t_name  from TheaterDetail as a 
    Inner join CityDetails b on a.c_id= b.c_id

select * from Tbl1 Order by c_id asc , iSrno asc

Upvotes: 1

Related Questions