我有一组三张表:
Dining_Tables;
+--------------------+--------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+--------------------+--------------+------+-----+---------+-------+
| dining_table | int(11) | NO | PRI | NULL | |
| bus_boy | varchar(35) | NO | | NULL | |
| waiter | varchar(35) | NO | | NULL | |
| server | varchar(35) | NO | | NULL | |
+--------------------+--------------+------+-----+---------+-------+
Poker_Tables;
+--------------------+--------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+--------------------+--------------+------+-----+---------+-------+
| poker_table | int(11) | NO | PRI | NULL | |
| dealer | varchar(35) | NO | | NULL | |
| pit_boss | varchar(35) | NO | | NULL | |
+--------------------+--------------+------+-----+---------+-------+
Computer_Tables;
+--------------------+--------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+--------------------+--------------+------+-----+---------+-------+
| computer_table | int(11) | NO | PRI | NULL | |
| programmer | varchar(35) | NO | | NULL | |
+--------------------+--------------+------+-----+---------+-------+
这些行中的每一行都有与其关联的全局唯一表 ID:(dining_table
, poker_table
, computer_table
)
其他列存储完成该名单的人员的名字/姓氏。
在我的模型中,一个人可以做多件事。例如,乔·史密斯可能同时坐在一个computer_table
作为一名程序员,坐在poker_table
作为经销商,并等待dining_table
作为服务员。
我的问题是这样的:
我想要一个可以让我检索所有的查询table_ids
对于一个特定的人。具体来说,查询将返回一个列表table_ids
乔·史密斯目前所在的职位。
我可以按照这些思路做一些事情:
select dining_table from Dining_Tables where
bus_boy = "Joe Smith" or
waiter = "Joe Smith" or
server = "Joe Smith";
select poker_table from Poker_Tables where
dealer = "Joe Smith" or
pit_boss = "Joe Smith";
select computer_table from Computer_Tables where
programmer = "Joe Smith";
但是,这是三个单独的查询,如果可能的话,我真的希望避免这样做。我可以使用联接在一个查询中完成此操作吗?
您的数据模型不是最优的。考虑:
Person PersonRole Role Table
------ ---------- ---- -----
Id* PersonId* Id* Id*
Name RoleId* Name Name
TableId
话虽如此...
select dining_table from Dining_Tables where
bus_boy = "Joe Smith" or
waiter = "Joe Smith" or
server = "Joe Smith"
union
select poker_table from Poker_Tables where
dealer = "Joe Smith" or
pit_boss = "Joe Smith"
union
select computer_table from Computer_Tables where
programmer = "Joe Smith"
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)