我有两个表products
和users
字段
产品
id
name
insertedUserID
ipdatedUserID
用户
id
name
我正在尝试使用数据获取产品详细信息,
ProductID
ProductName
InsertedUser
UpdatedUser
为此我使用这个mysql查询,
SELECT Products.id as ProductID,Products.name as ProductName, Users.name as InsertedUser
LEFT JOIN Users ON Products.insertedUserID = Users.id
此查询仅适用于“已插入的用户”。如何通过此查询获得更新用户?
答案 0 :(得分:0)
我不确定您是否希望同时获取数据或条件。如果您想同时调整数据,请使用 union all
SELECT Products.id as ProductID,Products.name as ProductName, Users.name as InsertedUser
LEFT JOIN Users ON Products.insertedUserID = Users.id
union all
SELECT Products.id as ProductID,Products.name as ProductName, Users.name as InsertedUser
LEFT JOIN Users ON Products.ipdatedUserID = Users.id
或者如果你只想单一条件那么
SELECT Products.id as ProductID,Products.name as ProductName, Users.name as InsertedUser
LEFT JOIN Users ON Products.insertedUserID = Users.id and Products.ipdatedUserID = Users.id
希望它会帮助你