使用select查询获取插入的用户名和更新的用户名的Mysql查询?

时间:2017-11-01 06:19:38

标签: mysql join left-join

我有两个表productsusers字段

产品

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

此查询仅适用于“已插入的用户”。如何通过此查询获得更新用户?

1 个答案:

答案 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

希望它会帮助你