So I have three tables. 1: Business - consists of a id and name, 2: Money - consists of person id and money they make, 3: Person - consists of person id and the business id that they work at.
I'm trying to go through all of the rows in business and insert into a new table the total money the business owes. I can easily get the sum of what a business owes total by doing a join on the money table and business table.
What I want to do is loop through all of the businesses and get the id and then use that id in my sql query to get the total money per business.
So I have something like "insert into business_payout (business_id,money_owed,date) values (Need to get all business id's here,I have the appropriate query already built but I don't know how to update it with the current business id,CURDATE())"
I'm pretty new to sql but what I've seen online is that a cursor may be appropriate. I was just wondering if this is the most efficient way to do this or is there something I'm missing.
Thanks for any help!
EDIT: UPDATED WITH TABLE STRUCTURE
Business: business_id int, business varChar
Money: person_id int, money float
Person: person_id int,business_id int
Sorry it isn't more detail/better format I tried to copy and paste from sql but it formatted it horribly on stack overflow.
Mysql<>Sql server. Please TAG the one you are using