2017-11-01 10 views
0

나는 두 테이블 products 및 필드 users,삽입 쿼리를 사용하여 삽입 된 사용자 이름과 업데이트 된 사용자 이름을 가져 오는 MySQL 쿼리?

제품

id 
name 
insertedUserID 
ipdatedUserID 

사용자

id 
name 

나는 데이터와 제품 상세 정보를 얻기 위해 노력하고,

이 나는이 MySQL의 쿼리를 사용하고이를 위해

,

SELECT Products.id as ProductID,Products.name as ProductName, Users.name as InsertedUser 
LEFT JOIN Users ON Products.insertedUserID = Users.id 

이 쿼리는 삽입 된 사용자 만 노력하고 있습니다. 이 쿼리로 업데이트 된 사용자를 얻으려면 어떻게해야합니까?

답변

0

데이터 및 조건을 모두 가져오고 싶지는 않습니다. 둘 다 상태 데이터를 원하는 경우 다음 조합을 사용하는 모든

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 

내가 여기에 필요한 것을 의심 그것은 당신에게

+0

을 도움이되기를 바랍니다 : LEFT JOIN Users on Products.insertedUserID = Users.id 및 Products.ipdatedUserID = Users.id'는 "and"가 아닌 "OR"입니다. – Wodin