Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6013001
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T02:29:06+00:00 2026-05-23T02:29:06+00:00

This is a more detailed question as my previous attempt wasn’t clear enough. I’m

  • 0

This is a more detailed question as my previous attempt wasn’t clear enough. I’m new to MySQL and have no idea about the best way to do certain things. I’m building a voting application for images and am having trouble with some of the finer points of MySQL

My db

_votes

  • id
  • voter_id
  • image_id

_images

  • id
  • file_name
  • entrant_id
  • approved

_users

  • id
  • …

Basically I need to do the following:

  1. tally up all votes that are approved
  2. return the top 5 with the most votes
  3. check if the user has voted on each of these 5 (return Boolean) from another table

I’ve tried variations of

SELECT i.id, i.file_name, i.total_votes
FROM _images i WHERE i.approved = 1 
CASE WHEN (SELECT count(*) from _votes v WHERE v.image_id = i.id AND v.voter_id = ?) > 0 THEN '1' ELSE '0' END 'hasvoted'
ORDER BY i.total_votes DESC LIMIT ".($page*5).", 5

is that something I should try and do all in one query?

This query was working fine before I tried to add in the ‘hasvoted’ boolean:

SELECT id, file_name, total_votes FROM _images WHERE approved = 1 ORDER BY total_votes DESC LIMIT ".($page*5).", 5

At the moment I’m also storing the vote count in the _images table and I know this is wrong, but I have no idea about how to tally the votes by image_id and then order them.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T02:29:07+00:00Added an answer on May 23, 2026 at 2:29 am

    Let me give this a shot to see if I understand your question:

    SELECT i.*,(SELECT COUNT(*) FROM _votes WHERE i.id = image_id) AS total_votes, (SELECT count(*) from _votes where i.id = image_id and user_id = ?) as voted  FROM _images AS i WHERE i.approved = 1
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This is a more detailed version of the same question asked yesterday. I have
Ok, I'm going to try to make this more clear because my last question
I've asked very similar question before and should have mentioned more detailed. Last time
To make this more clear, I'm going to put code samples: $file = fopen('filename.ext',
I have a label on my asp.net page, it looks like this: more info
This is more of an academic inquiry than a practical question. Are there any
This is more of an generic XML Schema question, but if and how do
This is more an observation than a real question: MS-Access (and VBA in general)
This is more of a syntax question I'm trying to write a store procedure
This is more of a business-oriented programming question that I can't seem to figure

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.