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

在查看表中的记录时,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)  |
+----------+--------------+

最佳答案

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

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

09-09 19:28