I have two tables named board_votes and req_form. When a form_id in the board_votes table is equal to 2 (i.e two board member votes means approved) I want to change the value of the column Board_approval in the req_form table to ‘yes’.
Each Board_approval in the req_form table is set at ‘pending’ and only on atleast two form_id entries into the board_votes can the Board_approval value be changed to ‘yes’.
board_votes table:
+---------+------------+----------+
| vote_id | form_id | board_id |
+---------+------------+----------+
| 1 | 22 | 1 |
| 3 | 22 | 3 |
| 4 | 26 | 1 |
| 5 | 7 | 1 |
| 6 | 19 | 1 |
+---------+------------+----------+
req_form:
+-------------------------+
| form_id Board_approval |
+-------------------------+
| 7 pending |
| 19 pending |
| 22 pending |
| 23 pending |
+-------------------------+
Here’s the sql query I’m using also:
"Update req_form
SET Board_approval = 'yes'
HAVING COUNT(board_votes.form_id = '22') >1";
>Solution :
UPDATE rf
SET rf.BoardApproval = 'Yes'
FROM req_form rf
JOIN
(
SELECT form_id FROM board_votes bv
GROUP BY bv.form_id
HAVING COUNT(bv.vote_id) >= 2
) TwoVotes ON TwoVotes.form_id = rf.form_id