如何一次只抓取一条记录?

How to grab just 1 record in a single pass?

我有一个 MySQL table 看起来像这样:

recordID | peopleID | AddressTypeID | Address | ActiveInd
10       | 102      | 1             | 4th Ave | 1
11       | 102      | 3             | 4th Ave | 1
12       | 203      | 3             | 5th Ave | 1

我正在尝试在 AddressTypeID = 1 的任何地方获取 peopleID 级别的记录,但如果不存在,则获取 AddressTypeID = 3 的记录。

所以结果集是这样的:

recordID | peopleID | AddressTypeID | Address | ActiveInd
10       | 102      | 1             | 4th Ave | 1
12       | 203      | 3             | 5th Ave | 1

我不认为 coalesce 是答案,并想尝试一个大的 case 语句,但我会使用 case 得到重复的记录,对吗?

我的答案的修改版本(处理故障转移语言):

select t.*
from mytable t
left join mytable t1
    on  t1.peopleID = t.peopleID
    and t1.AddressTypeID = 1
    and t.AddressTypeID  = 3
where t.AddressTypeID in (1, 3)
  and t1.recordID is null

我更改了示例数据以涵盖更多案例:

| recordID | peopleID | AddressTypeID | Address | ActiveInd |
|----------|----------|---------------|---------|-----------|
|       10 |      102 |             1 | 4th Ave |         1 |
|       11 |      102 |             3 | 4th Ave |         1 |
|       12 |      203 |             3 | 5th Ave |         1 |
|       13 |      304 |             1 | 6th Ave |         1 |
|       14 |      405 |             2 | 7th Ave |         1 |

结果是:

| recordID | peopleID | AddressTypeID | Address | ActiveInd |
|----------|----------|---------------|---------|-----------|
|       10 |      102 |             1 | 4th Ave |         1 |
|       12 |      203 |             3 | 5th Ave |         1 |
|       13 |      304 |             1 | 6th Ave |         1 |

演示:http://sqlfiddle.com/#!9/d48b92/2