MySQL:跨多个表的多列连接?

Run*_*ble 3 mysql join

我有一组三个表:

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_idsJoe Smith当前所在的列表.

我可以沿着这些方向做点什么:

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";

但是,这是三个单独的查询,我真的更愿意尽可能避免这样做.我可以使用连接在一个查询中执行此操作吗?

Tom*_*lak 7

您的数据模型不是最佳的.考虑:

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"
Run Code Online (Sandbox Code Playgroud)


The*_*TXI 5

如果要在单个查询中返回它,可以使用UNION语句将这三个查询合并为一个.

select dining_table as Table from Dining_Tables where 
      bus_boy = "Joe Smith" or
      waiter = "Joe Smith" or 
      server = "Joe Smith"
UNION
select poker_table as Table from Poker_Tables where 
      dealer = "Joe Smith" or 
      pit_boss = "Joe Smith"
UNION
select computer_table as Table from Computer_Tables where 
      programmer = "Joe Smith"
Run Code Online (Sandbox Code Playgroud)

虽然它是三个语句,但它将作为单个结果集返回.

如果将"作业"分成单独的表,则可以使查询更加容易.

我可以想象出以下几点:

表,表类型,人员,角色,表格

TableTypes
ID    TypeName
-----------------------
1     Dining
2     Poker
3     Computer

Tables
ID    TableTypeID
----------------------------
1     1
2     1
3     2
4     3
5     2
6     3

Persons
ID    Name
---------------------------
1     Joe Smith
2     Mary Belcher
3     Norma Grey


Roles
ID    RoleName
-----------------------------
1     Bus Boy
2     Waiter
3     Server
4     Dealer
5     Pit Boss
6     Programmer


TableRoles
TableID    RoleID    PersonID
-----------------------------
1          1         1
//etc
//etc
Run Code Online (Sandbox Code Playgroud)

这将使您的所有查询变得非常简单和直接,并使其可扩展(因此您可以在不破坏数据库的情况下为每个表添加更多角色)