本文介绍了海友我对Sql Server查询有疑问的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一张桌子像



table1

i have a table like

table1

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





table2



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



table3

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





i想查看桌子





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告诉我thankx



plz tell me thankx

推荐答案

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


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


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


这篇关于海友我对Sql Server查询有疑问的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-10 13:20