テーブルitems
id maxVotes parent type
10 2 9 heading
11 0 10 item
12 0 10 item
テーブルvotes
userId votedFor parent
1 11 10
1 12 10
2 12 10
アイテムが存在するかどうか、およびユーザーがその見出しの下で許可されているアイテムの最大数に投票したかどうかを確認しようとしています。
上記の例では、テーブルitems
にアイテムが含まれています。テーブルvotes
には、ユーザーが投じた投票が含まれています。
テーブルitems
type : heading
は、ユーザーが投票できるアイテムの最大数を指定しますcol : maxVotes
。この場合は2
です。
表votes
user 1
で は 2 つの項目に投票しましitems
たが、その見出しの下にある項目にはこれ以上投票できません。ユーザー 2 は、あと 1 アイテムに投票できます。
それはそのように続きます。
私が現在行っている方法(phpを使用)は、次のとおりです。
select id, parent from items where id = 11 //The item exists.
select maxVotes from items where id = parent // This gives me the maximum items a user can vote for.
select count(votedFor) as votes from votes where userId = 1 // This'll give me 2.
User 1 can vote no more, but user 2 can vote once more -> Add his vote to the votes table
上記の方法以外に、これを行うためのより簡単で効率的で洗練された方法を考えられますか?
これはまだ実装されていないため、変更を加えることができます。それとも、これが最善の方法ですか?