从sql查询中获取空结果

时间:2015-01-07 16:57:45

标签: sql select join

我有这个问题:

select p1.cost as V1, p2.cost as V2 , p3.cost as V3 
from product p1, product p2, product p3 
where p1.id =1 and p2.id =2 and p3.id=3

在我的产品表中,我只有一行:

id   | name       | cost
1    | product1   | 20

正如你在这里看到的,我会得到空的结果! 但是我怎么做才能得到:

V1   | V2  | V3
20   |     |

1 个答案:

答案 0 :(得分:1)

您可以使用左连接:

SELECT    p1.cost AS V1, p2.cost AS V2 , p3.cost AS V3 
FROM      product p1
LEFT JOIN product p2 on p2.id = 2
LEFT JOIN product p3 on p3.id = 3
WHERE     p1.id = 1