I want to set a cron job that will execute every 15 minutes. Before going into deep description of these query I’ll explain how my database works.
I have 2 tables ‘stories‘ and ‘votes‘. All information about article like title, category etc. is stored in the ‘stories‘ table. It’s not a big surprise that all the votes related to article are stored in a ‘votes‘ table. In the votes table there is a field named ‘item_name‘ which corespondents to the ‘id‘ of article that is in ‘stories’ table. Finally, in ‘stories‘ table I have a field named ‘showing‘ which is by default set to 0. And I want to change it to different values depending on how many votes article has, but more about it latter.
Here is a structure of my database:
Stories table

Votes table

Ok now when I cleared database structure lets get down to my question. I need a query that would look at all articles where showing=0 and sum up votes that corespondent to a particular article. If sum of votes = 10 than set showing=1 if sum of votes = -10 than set showing=2. I don’t know how such query would look, I really don’t :/
So could anyone suggest a solution please?
Addition: I asked similar question before and some people suggested trigger’s or updating database each time a vote is cast. But I would rather check it every 15 min with cron job. )))
EDIT: Just to give you more understanding of how it works )))
For example this row from ‘stories’ table:
id| 12
st_auth | author name
st_date | story date
st_title| story title
st_category| story category
st_body| story body
showing| 0 for pending, 1 for approved and 2 for unapproved
Co responds to this one from ;votes’ table:
id| 83
item_name| 12 (id of article)
vote_value| 1 for upvote -1 for downvote
etc…
This should give you all the articles with showing 0 and total vote value. After that you can process them row by row and update voting if needed.
Added:
I am not a PHP programmer, I mostly do PERL, but I guess it has to be something like this: