Click here to Skip to main content
15,885,244 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i have a table like

table1
t1id name age gender
1    abc  24  m
2    cde  54  F


table2
t1id type solution t31
1    xyz   123      
1    ghi   242
1    jkh   254     x1
2    xyz   425      
2    ghi   543
2    jkh   545     x2


table3
t31 date 
x1  15,mar,2015
x2  16,mar,2015


i would like to view tha table

name gender age type solution date
abc   m     24  xyz  123      
abc   m     24  ghi  242
abc   m     24  jkh  254      15,mar,2015
cde   F     54  xyz  425      
cde   F     54  ghi  543
cde   F     54  jkh  545      16,mar,2015


plz tell me thankx
Posted
Updated 15-Mar-15 20:38pm
v6
Comments
PIEBALDconsult 16-Mar-15 2:39am    
Learn to JOIN (INNER and LEFT OUTER).
And I really hope you are not storing dates as strings.

Try like below. But have to tried a solution? Question seems to be basic left join

SQL
DECLARE @Table1 TABLE (
t1id int, name varchar(30), age int, gender char(1))
INSERT INTO @Table1 VALUES (1, 'abc', 24, 'm')
                           , (2, 'cde', 54, 'F')

DECLARE @Table2 TABLE (t1id int,  type varchar(30), solution int, t31 varchar(3))
INSERT INTO @Table2 VALUES (1, 'xyz', 123, '')
,(1, 'ghi', 242,'')
,(1, 'jkh', 254, 'x1')
,(2, 'xyz', 425, '')
,(2, 'ghi', 543,'')
,(2, 'jkh', 545, 'x2')

DECLARE @Table3 TABLE (t31 varchar(3), date date)
INSERT INTo @Table3 VALUES ('x1', '15-mar-2015')
                        ,  ('x2', '16-mar-2015')


SELECT name, age, gender,type, solution, date
FROM @Table1 t1
JOIN @Table2 t2 ON t2.t1id = t1.t1id
LEFT JOIN @Table3 t3 ON t3.t31 = t2.t31
 
Share this answer
 
Comments
Parazival 16-Mar-15 3:05am    
name gender age type solution date
abc m 24 xyz 123 15,mar,2015
abc m 24 ghi 242 15,mar,2015
abc m 24 jkh 254 15,mar,2015
cde F 54 xyz 425 16,mar,2015
cde F 54 ghi 543 16,mar,2015
cde F 54 jkh 545 16,mar,2015


IT WIILL SHOW LIKE ABOVE
Saral S Stalin 16-Mar-15 3:30am    
I am a bit confused at what you want now. Please update the question with your exact requirement.
CHill60 16-Mar-15 4:55am    
Answers problem as originally presented by OP. Note however date and type are reserved words so should really be surrounded by [ ]. Use of the table variables may have confused the OP but this works either way. A 4-vote from me.
Hi,

Check this...
SQL
SELECT name, gender, age, type, solution, date FROM table2 B left outer join table1 A on  A.t1id=B.t1id  
left outer  table2 C on B.t31=C.t31


Hope this will help you.

Cheers
 
Share this answer
 
v3
Comments
CHill60 16-Mar-15 4:50am    
Almost but not quite ... missed out the word "join" on the final join. Also type and date are reserved words so should be surrounded with [] i.e.
SELECT name, gender, age, [type], B.solution, [date]
FROM table2 B left outer join table1 A on A.t1id=B.t1id
left outer join table3 C on B.t31=C.t31
Magic Wonder 16-Mar-15 5:13am    
Okay....i have not tested it.
I did not test it, But it should work.

SELECT 
	table1.name AS name, 
	table1.gender AS gender,
	table1.age AS age,
	table2.[type] AS [type]
	tabel2.solution AS solution,
	table3.[date] AS [date]
FROM table1
	INNER JOIN table2 ON table1.t1id = table2.t1id
	LEFT JOIN table3 ON table2.t31 = table3.t31
 
Share this answer
 
Comments
CHill60 16-Mar-15 4:56am    
No different to Solution 1 and 2 - you've just added the (redundant) column aliases

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