来自另一个表的两个元组连接到来自一个表的一个元组

时间:2019-12-17 16:54:49

标签: mysql sql

我有两个表:用户运单。运单中的每个元组对应于用户的两个元组。

在查看表中的记录时, loginManager 值显示在 loginManager loginStorekeeper 列中。

我可以修改查询以使店主和经理的名称不重复吗,还是需要向数据库中添加新表?

重复

User.Name 字段,因为店主和经理的姓名在同一张表中

我的查询是

SELECT Waybill.Id
     , Waybill.IMO
     , Vessel.Name
     , Waybill.loginManager
     , User.Name //manager login and name
     , Waybill.loginStorekeeper
     , User.Name
     , Waybill.Date //storekeeper login and name
  FROM Waybill
     , Vessel
     , User 
 WHERE Waybill.IMO = Vessel.IMO
   AND Waybill.loginManager = User.Login
   AND Waybill.loginManager = User.Login
 ORDER 
    BY Waybill.Id

运单

+-------------------+--------------+
| id                | INT(11)      |
| IMO               | INT(11)      |
| LoginManager      | VARCHAR(300) |
| LoginStoreKeeper  | VARCHAR(300) |
| Date              | DATETIME     |
+-------------------+--------------+

用户

+----------+--------------+
| Login    | VARCHAR(300) |
| Password | VARCHAR(300) |
| Name     | VARCHAR(300) |
| Phone    | VARCHAR(20)  |
| Role     | VARCHAR(50)  |
+----------+--------------+

1 个答案:

答案 0 :(得分:0)

无需添加表,就可以解决以下问题:

SELECT WaybillTable.Id
, WaybillTable.IMO
, VesselTable.Name
, WaybillTable.loginManager
, UserManagerTable.Name
, WaybillTable.loginStorekeeper
, UserStorekeeperTable.Name
, WaybillTable.Date 
FROM Waybill WaybillTable 
INNER JOIN Vessel VesselTable 
ON WaybillTable.IMO = VesselTable.IMO 
INNER JOIN User UserManagerTable 
ON WaybillTable.loginManager = UserManagerTable.Login 
INNER JOIN User UserStorekeeperTable 
ON WaybillTable.loginStorekeeper = UserStorekeeperTable.Login 
ORDER BY WaybillTable.Id