サブクエリは複数の値を返しました。サブクエリが =、!=、<、<=、>、>= の後に続く場合や、サブクエリが 1 つの式として使われる場合は複数の値は許可されません。
自己結合するデータに関して、select文を書いてから条件だけupdateに付け替えようとするとこのエラーが出る なんでselectのときは大丈夫なんだろう?
記録
テスト用のテーブル
table:content_history
user_id content (略) updated(timestamp型)
1 aa 000...AFA
2 Hello 000...AFB
1 あ 000...AFC
1 test 000...AFD
1 こんにちは 000...AFE
1 今日は 000...AFF
user_idごとの最新行を自己結合で取得するselect
code:sql
select
user_id ,content, updated
from
content_history
where
updated = (
select
max(updated) as max
from
content_history as c
where
user_id = c.user_id
group by c.user_id
)
order by user_id asc
;
これは実行できる
table:result
user_id content (略) updated
1 今日は 000...AFF
2 Hello 000...AFB
selectと同じ条件のwhereだとこのエラーになる
code:ng.sql
update
content_history
set
content = 'hoge'
where
updated = (
select
max(updated) as max
from
content_history as c
where
user_id = c.user_id
group by c.user_id
)
;
inで一致させると実行できる
code:ok.sql
update
content_history
set
content = 'hoge'
where
updated in (
select
max(updated) as max
from
content_history as c
where
user_id = c.user_id
group by c.user_id
)
;