如何从两个表中选择所有值

时间:2016-03-31 09:07:51

标签: sql-server sql-server-2008 sql-server-2005 sql-server-2012

 tableA                   tableB
IdPrice      price             id         tax          IdPrice
----------------------       ------------------------------------
  4          100               1          20              4
------------------------     ------------------ ------------------
  5          150               2          10              6
------------------------     ------------------ ------------------
  6          270            
------------------------
result =
price   id   tax
----   ---   ----
100      1    20
150      2    10
270    null  null
my Query
SELECT   price,id,tax
FROM  tableB INNER JOIN
tableA ON tableA.IdPrice= tableB.IdPrice
but this result 

price   id   tax
----   ---   ----
100      1    20
150      2    10

1 个答案:

答案 0 :(得分:1)

SELECT 
    a.price as price, b.id as id, b.tax as tax
FROM  
    tableA a 
LEFT OUTER JOIN 
    tableB b ON a.IdPrice = b.IdPrice

使用左外连接,您可以从tableA获取所有记录。