I have a table called users where the employee data are stored.
Also I have another table payment_details where employee’s payment related data are stored
the two tables are as follows.
this table is users
sr_no emp_no username payment
1 1001 leroy <null>
2 1003 harry <null>
3 1004 Tom <null>
4 1008 Jon <null>
This table below is payment_details
sr_no name number month status date
43 Jon 1008 January paid 5/16/2012
44 Jon 1008 January balance 5/16/2012
45 Harry 1003 January paid 5/16/2012
46 Tom 1004 January paid 5/16/2012
47 leroy 1001 January paid 5/16/2012
48 Jon 1008 January paid 5/16/2012
49 Harry 1003 January paid 5/16/2012
50 Jon 1008 February balance 5/16/2012
51 leroy 1001 February paid 5/16/2012
52 Jon 1008 February paid 5/16/2012
53 Tom 1004 February balance 5/16/2012
My question here is to update “users” table payment column to “paid” when the status of his/her is all paid in payment_details table
You can either do this: http://www.sqlfiddle.com/#!3/db13f/18
Or this: http://www.sqlfiddle.com/#!3/db13f/19
Their difference is how many rows it updates. On the second query, it updates all users, regardless if the user has a paid all status(
'paid') or not(null); on the first query, it updates only those who are paid.The advantage of the second query, is when you change one of the all
'paid'statusonpayment_detailof a given user to ‘not paid’ for example, it can revert back the user’spaymentstatus tonull