i have a table named Groups that have an id and maxUsers.
i also have a Scores table that have id, groupId and userId
i want to find a row in groups that have less than maxUsers rows with the same group id in scores and that non of the rows have the same userid as ? input
so i assumed when ever i put the column id in the bottom query it will reffer to the id in the current groups row, i dont think that this is happening right now, is it possible to make it work like i intend? and is there a way to make it work more efficiently?
select id from Groups where
(select count(1) from Scores where groupId = id) < maxUsers and
(? not in (select userId from Scores where groupId = id)) limit 1;