I have two tables in my SQL database, one for the photos that users sent aned another one for the votes wich photo had on my application. I need to extract the 30 photos from the 'photos' table wich had more votes on the 'votes' table. Is there a way to do it withgin a single query?
Thanks in advance.
You should be able to use a query like this:
select
a.photoFileName
from
photos a
join votes b
on a.photoId=b.photoId
order by
b.voteCount desc
limit 30
Adjust the keys to your exact column names on the linked fields.
This assumes that the votes table has an number column (voteCount) that has a tally of the votes for that image.
Something like this ( if each vote is stored single ), but make your own adjustments:
SELECT
p.id,
COUNT( v.id )
FROM
photos p
JOIN
votes v ON p.id = v.photo_id
ORDER BY
COUNT( v.id ) DESC
GROUP BY
v.photo_id
LIMIT 30;
PS: I did not test the query, just gave you an example!