1

我有两张桌子:

person
id
name

car
name
person_id

目前查询如下:

SELECT
person.name
car.name
FROM
person, car
WHERE
person.id = car.person_id

现在,如果有一辆车与一个人相关联,这可以正常工作。但是,如果没有,则该人将不会被退回。如果一个人没有车,我想用'No Car'column中的文本返回一行car.name

4

3 回答 3

3
select person.name, coalesce(car.name, 'no car')
from person
left outer join car on person.id = car.person_id
于 2013-02-01T13:22:16.047 回答
0
SELECT person.name, CASE WHEN car.name IS NULL THEN 'no car' ELSE car.name END 
FROM person
LEFT JOIN car ON person.id = car.person_id
于 2013-02-01T17:05:02.317 回答
-1
select person.name, ISNULL(car.name, 'no car')
from person
left join car on person.id = car.person_id
于 2013-02-01T13:23:13.880 回答