从三个表sql获取记录

时间:2013-04-28 13:52:12

标签: sql sql-server

我正在使用这三个表,我想要table3详细信息table_1.field_id。现在我正在获得没有table_1.field_id的完整记录。如何将table1.field_idtable_3记录包括在内?

查询:

select 
   fld_id, fld_name, fld_date 
from 
   table_3
where 
   fld_item_id in (select fld_id 
                   from tbl_table2 
                   where fld_cust_id in (select fld_id 
                                         from table_1 
                                         where fld_ref_id = 100));

表3

FLD_ID FLD_Name FLD_DATE     FLD_ITEM_ID
101       hai   12-01-2013   10

表2

FLD_ID FLD_value FLD_CUST_ID  FLD_DATE     
10       100     10000        12-01-2013

表1

FLD_ID FLD_NAME fld_ref_id  FLD_DATE
10000  Vinoth   100         12-01-2013

1 个答案:

答案 0 :(得分:2)

尝试(按照Barry Young的建议)

SELECT B.fld_cust_id Table1FieldId, C.fld_id, C.fld_name, C.fld_date 
FROM tbl_table2 B
INNER JOIN table_3 C ON C.fld_item_id = B.fld_id
WHERE A.fld_ref_id=100

或(如果您需要table_1中的其他字段)

SELECT A.fld_id Table1FieldId, C.fld_id, C.fld_name, C.fld_date 
FROM table_1 A
INNER JOIN tbl_table2 B ON B.fld_cust_id = A.fld_id 
INNER JOIN table_3 C ON C.fld_item_id = B.fld_id
WHERE A.fld_ref_id=100