SQL 2008: 我试图从具有相同字段但不同数据的两个表(TODO(T)和TODO_OPERATOR(Z))获取数据(即合并),然后在合并数据上使用2个内部联接。不知道怎么做。例如,提供的@id参数将为001。您是否先以某种方式合并选择T和Z表,然后进行INNER JOINS?
SELECT
T.*, Z.*, TT.lookup_desc todo_type_desc, TS.lookup_desc status_desc
FROM TODO T
CROSS JOIN TODO_OPERATOR Z
INNER JOIN LOOKUP TT ON T.todo_type=TT.lookup_id, Z.todo_type=TT.lookup_id
INNER JOIN LOOKUP TS ON t.status=TS.lookup_id, z.status=TS.lookup_id
WHERE id=@id
简化的表格字段为:
TODO和TODO_OPERATOR:
id,status,todo_type
LOOKUP:
lookup_id,lookup_desc
由于
答案 0 :(得分:1)
选择
一个。*
,TT.lookup_desc todo_type_desc
,TS.lookup_desc status_desc
FROM
(从TODO中选择id,status,todo_type UNION ALL从TODO_OPERATOR中选择id,status,todo_type)A
INNER JOIN LOOKUP TT ON A.todo_type = TT.lookup_id
INEN JOIN LOOKUP TS ON A.status = TS.lookup_id
WHERE id = @ id
答案 1 :(得分:0)
SELECT
T.*, Z.*, TT.lookup_desc todo_type_desc, TS.lookup_desc status_desc
FROM TODO T
INNER JOIN TODO_OPERATOR Z
ON Z.todo_type = T.todo_type
AND z.status = T.status
INNER JOIN LOOKUP TT ON T.todo_type=TT.lookup_id
INNER JOIN LOOKUP TS ON T.status=TS.lookup_id
WHERE id=@id
我不确定这对你有帮助,你提供的信息有点小。