0

我想查询下表以返回 SessionId 和 Roomx 的交集为 0 的列的标题。 Room 的类型是 TINYINT - 在 mysql 中表示 BOOLEAN

下面是表格:

SessionId    Room1    Room2    Room3
    1          0        1        0  
    2          1        0        1

对于上表,例如 SessionId 1 ,查询应返回 Room1 和 Room3 帮助将不胜感激。谢谢。

4

2 回答 2

1

It looks to me like a table in need of a redesign. Judging by the column names, the "objects" (rows) you should be dealing with are "Events": An Event happens in a Room, during a section. With that setup, your table would look like:

session_id room_id
1          2
2          1
2          3

Now there's still not a query that'll give you the rooms that aren't in use during a given session, but it's easy to find the ones that are:

SELECT room_id FROM events WHERE session_id = <whatever>

And it's easy to combine that with a list of all possible rooms to get the information you need.

Hope this helps!

PS: If you have a rooms table as well (I wouldn't add one just for this, but if you happened to have one), it gets even easier:

SELECT id FROM rooms WHERE NOT EXISTS (SELECT * FROM events WHERE room_id = id AND session_id = <whatever>)
于 2011-02-10T23:45:21.560 回答
-2
SELECT column_name FROM information_schema.columns WHERE table_name = 'tabel Name' 
于 2011-02-08T06:53:33.317 回答