私の目標は 2 つのことを行うこと
です。
- userID が (特定の部屋で) 曲に対して少なくとも 1 票を持っているかどうかを調べるvotes
votes
songID
userID
とRoomID
がクエリに渡されます。それぞれを「ループ」する方法がわかりませんsongID
。2 つのクエリを実行しますか? 最初にそれぞれを取得し、次にループをsongID
実行して上記の情報を取得します (これには Java を使用します)。for
私の目標は 2 つのことを行うこと
です。
- userID が (特定の部屋で) 曲に対して少なくとも 1 票を持っているかどうかを調べるvotes
votes
songID
userID
とRoomID
がクエリに渡されます。それぞれを「ループ」する方法がわかりませんsongID
。2 つのクエリを実行しますか? 最初にそれぞれを取得し、次にループをsongID
実行して上記の情報を取得します (これには Java を使用します)。for
最初の質問でこれを行うことができます
SELECT Count(songID), songID
FROM votes
WHERE RoomID = @roomid
GROUP BY songID
そして、これは2番目の質問です
SELECT songID
FROM votes
WHERE UserID = @userid
AND RoomID = @roomid
select SongID, count(*) as voteCount,
case when exists(
select 1 from votes b
where b.songID = a.songId and b.RoomID = ? and b.UserID = ? )
then 'Yes' else 'No' end case as didUserVote
from votes a
where a.RoomID = ?
group by SongID
それでもうまくいかない場合は、これを試してください。ユーザーがその曲に投票しなかったUserID
場合、および投票した場合、3 番目の列は null になります。
select a.SongID, count(*) as voteCount, b.UserID
from votes a left join votes b on a.songID = b.songID and b.RoomID = ? and b.UserID = ?
where a.RoomID = ?
group by a.SongID, b.UserID
retunr が NULL の場合は、3 列目でこれを試してください。投票はありません。
select SongID, count(*) as voteCount,(
select b.userid from votes b
where b.songID = a.songId and b.RoomID = 131 and b.UserID = 70)
from votes a
where a.RoomID = 131
group by SongID