$sql ="(SELECT Drive.DriveID,Ram.Memory from Drive,Ram where Drive.DriveID = Ram.RamID) UNION
(SELECT Drive.DriveID,External.Memory from Drive, External where Drive.DriveID = External.ExtID)";
假设我也想获得Ram.Name。我该怎么做呢?如果我在第一个SELECT语句中使用Ram.Name,则不会产生正确的结果。
解决这个问题的方法有哪些?我想用UNION来做。
答案 0 :(得分:2)
怎么样:
SELECT Drive.DriveID,Ram.Memory,Ram.Name
FROM Drive,Ram WHERE Drive.DriveID = Ram.RamID
UNION
SELECT Drive.DriveID,External.Memory,NULL
FROM Drive, External WHERE Drive.DriveID = External.ExtID
对于第二个选择
的所有结果,第四列中将为NULL