我需要从这3个表中选择Building,RoomMin和RoomMax,并将它们显示在不同的字段中。
表
GenericTableDataID Building
17 B1
18 B1
20 B1
RoomMinDataID RoomMin
17 200
20 100
RoomMaxDataID RoomMax
17 299
18 399
期望的结果
GenericTableDataID Building RoomMin RoomMax
17 B1 200 299
尝试失败:
SELECT GenericTableData.GenericTableDataID, ValueString AS Building, ValueString AS RoomMin, ValueString AS RoomMax
FROM GenericTableData INNER JOIN
GenericTableDataField ON GenericTableData.GenericTableDataID = GenericTableDataField.GenericTableDataID INNER JOIN
GenericTableDefinition ON GenericTableData.GenericTableDefinitionID = GenericTableDefinition.GenericTableDefinitionID INNER JOIN
GenericTableDefinitionField ON GenericTableDataField.GenericTableDefinitionFieldID = GenericTableDefinitionField.GenericTableDefinitionFieldID AND
GenericTableDefinition.GenericTableDefinitionID = GenericTableDefinitionField.GenericTableDefinitionID
WHERE GenericTableDefinitionField.GenericTableDefinitionFieldID = 13 -- Access Level
AND GenericTableData.GenericTableDataID IN ( -- RoomMin
SELECT GenericTableData.GenericTableDataID
FROM GenericTableData INNER JOIN
GenericTableDataField ON GenericTableData.GenericTableDataID = GenericTableDataField.GenericTableDataID INNER JOIN
GenericTableDefinition ON GenericTableData.GenericTableDefinitionID = GenericTableDefinition.GenericTableDefinitionID INNER JOIN
GenericTableDefinitionField ON GenericTableDataField.GenericTableDefinitionFieldID = GenericTableDefinitionField.GenericTableDefinitionFieldID AND
GenericTableDefinition.GenericTableDefinitionID = GenericTableDefinitionField.GenericTableDefinitionID
WHERE ValueInteger <= '235'
AND GenericTableData.GenericTableDataID IN ( -- List of data that are associated with B1
SELECT GenericTableData.GenericTableDataID
FROM GenericTableData INNER JOIN
GenericTableDataField ON GenericTableData.GenericTableDataID = GenericTableDataField.GenericTableDataID INNER JOIN
GenericTableDefinition ON GenericTableData.GenericTableDefinitionID = GenericTableDefinition.GenericTableDefinitionID INNER JOIN
GenericTableDefinitionField ON GenericTableDataField.GenericTableDefinitionFieldID = GenericTableDefinitionField.GenericTableDefinitionFieldID AND
GenericTableDefinition.GenericTableDefinitionID = GenericTableDefinitionField.GenericTableDefinitionID
WHERE ValueString LIKE 'B1'
)
AND GenericTableDefinitionField.GenericTableDefinitionFieldID = 11
)
AND GenericTableData.GenericTableDataID IN ( -- RoomMax
SELECT GenericTableData.GenericTableDataID
FROM GenericTableData INNER JOIN
GenericTableDataField ON GenericTableData.GenericTableDataID = GenericTableDataField.GenericTableDataID INNER JOIN
GenericTableDefinition ON GenericTableData.GenericTableDefinitionID = GenericTableDefinition.GenericTableDefinitionID INNER JOIN
GenericTableDefinitionField ON GenericTableDataField.GenericTableDefinitionFieldID = GenericTableDefinitionField.GenericTableDefinitionFieldID AND
GenericTableDefinition.GenericTableDefinitionID = GenericTableDefinitionField.GenericTableDefinitionID
WHERE ValueInteger >= '235'
AND GenericTableData.GenericTableDataID IN ( -- List of data that are associated with B1
SELECT GenericTableData.GenericTableDataID
FROM GenericTableData INNER JOIN
GenericTableDataField ON GenericTableData.GenericTableDataID = GenericTableDataField.GenericTableDataID INNER JOIN
GenericTableDefinition ON GenericTableData.GenericTableDefinitionID = GenericTableDefinition.GenericTableDefinitionID INNER JOIN
GenericTableDefinitionField ON GenericTableDataField.GenericTableDefinitionFieldID = GenericTableDefinitionField.GenericTableDefinitionFieldID AND
GenericTableDefinition.GenericTableDefinitionID = GenericTableDefinitionField.GenericTableDefinitionID
WHERE ValueString LIKE 'B1'
)
AND GenericTableDefinitionField.GenericTableDefinitionFieldID = 12
)
我的上述查询不会返回任何行,因为我正在尝试在内部WHERE SELECTS中搜索特定的建筑物房间号。
答案 0 :(得分:0)
试试这个:
SELECT generic.GenericTableId
, Building
, RoomMin
, RoomMax
FROM GenericTableDataId generic
JOIN RoomMinDataId min_data ON generic.GenericTableDataId = min_data.RoomMinDataId
JOIN RoomMaxDataId max_data ON generic.GenericTableDataId = max_data.RoomMaxDataId
答案 1 :(得分:0)
SELECT A.GenericTableDataID, A.Building, B.RoomMin, C.RoomMax
FROM table_1 AS A
JOIN table_2 AS B ON
A.GenericTableID = B.RoomMinDataID
JOIN table_3 AS C ON
A.GenericTableID = C.RoomMaxDataID
WHERE A.GenericTableID = 17
table_1
是您问题的首页,table_2
是中间,table_3
是底部。
答案 2 :(得分:0)
作为“gr1zzly be4r”的帖子,您可以使用JOIN函数并使用一些别名
SELECT GenericTableDataID,Building,RoomMin,RoomMax
From table1
join table2 on
GenericTableDataID=RoomMinDataID
join table3 on
RoomMinDataID=RoomMaxDataID
where GenericTableDataID = 17
另外,另一个选择
select GenericTableDataID,Building,RoomMin,RoomMax
FROM table1,table2,table3
where GenericTableDataID=RoomMinDataID and RoomMinDataID=RoomMaxDataID and GenericTableDataID=17
答案 3 :(得分:0)
这是我的最终查询:
SELECT DISTINCT Data.GenericTableDataID AS AccessLevelID, TDRR.Building, TDRR.RoomMin, LTDRR.RoomMax, DataF.ValueString AS AccessLevel
FROM GenericTableData Data
INNER JOIN GenericTableDataField DataF ON Data.GenericTableDataID = DataF.GenericTableDataID
INNER JOIN GenericTableDefinition Def ON Data.GenericTableDefinitionID = Def.GenericTableDefinitionID
INNER JOIN GenericTableDefinitionField DefF ON DataF.GenericTableDefinitionFieldID = DefF.GenericTableDefinitionFieldID AND
Def.GenericTableDefinitionID = DefF.GenericTableDefinitionID
INNER JOIN TableDataRoomRanges TDRR ON TDRR.BuildingDataID = Data.GenericTableDataID
WHERE DefF.GenericTableDefinitionFieldID = 13
-- Query for TableDataRoomRanges view
SELECT TableDataBuildings.BuildingDataID, TableDataBuildings.Building, TableDataRoomMin.RoomMin, TableDataRoomMax.RoomMax
FROM
TableDataBuildings
INNER JOIN
TableDataRoomMin ON TableDataBuildings.BuildingDataID = TableDataRoomMin.RoomMinDataID
INNER JOIN
TableDataRoomMax ON TableDataBuildings.BuildingDataID = TableDataRoomMax.RoomMaxDataID
答案 4 :(得分:-1)
下面:
select
gt.GenericTableDataID,
gt.Building,
rmn.RoomMin,
rmx.RoomMax
from
GenericTable gt,
Roommin rmn,
Roommax rmx
where
gt.GenericTableDataID = rmn.RoomMinDataID
and gt.GenericTableDataID = rmx.RoomMaxDataID