0

我有多个我试图集成的 sql 语句:

1 -

SELECT b.name, b.address, b.city, b.state, b.zip
FROM b
WHERE b.my_ID =  '8'

2 -

SELECT DISTINCT room.room_ID, room.number, room.maxStudents
    FROM room
    WHERE room.building_ID = 
    (SELECT building_ID
    FROM building
    WHERE my_ID =  '8')

我希望将第一条语句嵌入到选择字段中,例如:

SELECT DISTINCT room.room_ID, room.number, room.maxStudents,
(SELECT b.name, b.address, b.city, b.state, b.zip
FROM b
WHERE b.my_ID =  '8')
FROM room
WHERE room.building_ID = 
(SELECT building_ID
FROM building
WHERE my_ID =  '8')

ROOM 表与 b 相关 - 每个房间都有一个字段 roo​​m.b_ID,例如:

room.b_ID = b.b_ID

我曾尝试在几个地方将其添加到综合声明中,但均未成功。

我想以所有房间和 room.fields 以及相关字段的列表结束,或者父母的字段 b.fields (b.name, b.address, b.city, b.state, b.zip )。b 是建筑。所以每个房间都有它所在的建筑物。我想要一份所有房间及其相关建筑物的列表。每栋建筑都会有不止一个房间,所以我知道结果中会有重复的数据。理想情况下,我想要一个 BUILDING 记录,然后该建筑物中的房间如下所示:

Building 1: Address, city,state,zip
<tab/null>room.name, room.maxstudents,...
<tab/null>room.name, room.maxstudents,...
<tab/null>room.name, room.maxstudents,...
Building 2: Address, city,state,zip
<tab/null>room.name, room.maxstudents,...
<tab/null>room.name, room.maxstudents,...

非常感谢任何帮助或指导!

4

1 回答 1

0

IFAIK,每个查询作为固定数量的列!

我的建议:在您的应用程序中使用类似于以下解析输出的查询:

Building 1, Address, city, state, zip, room.name1, room.maxstudents,...
Building 1, Address, city, state, zip, room.name2, room.maxstudents,...
Building 1, Address, city, state, zip, room.name3, room.maxstudents,...
Building 2, Address, city, state, zip, room.name1, room.maxstudents,...

对于此查询,请使用:

SELECT
    b.name, b.address, b.city, b.state, b.zip,
    room.room_ID, room.number, room.maxStudents
FROM b
LEFT JOIN room
ON room.building_ID = building.my_ID
ORDER BY building.name, room.room_ID;
于 2013-11-06T14:15:12.693 回答