-1

我有 3 个表:phones、phones_state 和 state

电话:

+------------------+
| phone_id | model |
+----------+-------+
| 1        | mot   |
| 2        | sam   |
+----------+-------+

手机状态:

+---------------------+
| phone_id | state_id |
+----------+----------+
| 2        | 1        |
| 2        | 3        |
| 2        | 5        |
| 1        | 1        |
+----------+----------+

状态:

+------------------+
| state_id | name  |
+----------+-------+
| 1        | rec   |
| 2        | notre |
| 3        | res   |
| 4        | wait  |
| 5        | back  |
+----------+-------+

我想获取所有处于状态 1 而不是处于状态 5 的手机如何在 mysql 查询中执行此操作?

4

1 回答 1

2
select p.phone_id
from phones p
inner join phones_state ps on ps.phone_id = p.phone_id
group by p.phone_id
having sum(ps.state_id = 1) >= 1 and sum(ps.state_id = 5) = 0 
于 2012-10-14T20:56:01.970 回答