senior
senior

Reputation: 2266

get empty result from sql query

I have this query:

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

In my product table I have just one row:

id   | name       | cost
1    | product1   | 20

As you can see here, I ll get empty result ! But how can I do to get:

V1   | V2  | V3
20   |     |

Upvotes: 1

Views: 49

Answers (1)

Mureinik
Mureinik

Reputation: 311018

You could use left joins:

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

Upvotes: 1

Related Questions