我有两张桌子,
write.csv(x, file = "c:\\myname\\yourfile.csv", row.names = FALSE)
输出应该是,
USER
USER_ID | USER_NAME
--------------------
659 | John
660 | Andrew
661 | Bianca
--------------------
USER_ADDRESS
USER_ID |TYPE | ADDRESS
------------------------------
659 | HOME | New York
659 | WORK | New Jersey
660 | HOME | San Francisco
660 | WORK | Fremont
------------------------------
如何在选择查询中获得上述输出?提前谢谢!
答案 0 :(得分:4)
试试这个:
SELECT u.*, uah.address as home_address, uaw.address as work_address
FROM users u
LEFT OUTER JOIN user_address uah
ON u.user_id = uah.user_id
AND uah.type = 'HOME'
LEFT OUTER JOIN user_address uaw
ON u.user_id = uaw.user_id
AND uaw.type = 'WORK'