我有两个表:用户和运单。运单中的每个元组对应于用户的两个元组。
在查看表中的记录时, 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) |
+----------+--------------+
答案 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