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 128397
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T05:37:44+00:00 2026-05-11T05:37:44+00:00

I have one table called gallery . For each row in gallery there are

  • 0

I have one table called gallery. For each row in gallery there are several rows in the table picture. One picture belongs to one gallery. Then there is the table vote. There each row is an upvote or a downvote for a certain gallery. Here is the (simplified) structure:

gallery ( gallery_id ) picture ( picture_id, picture_gallery_ref ) vote ( vote_id, vote_value, vote_gallery_ref ) 

Now I want one query to give me the following information: All galleries with their own data fields and the number of pictures that are connected to the gallery and the sumarized value of the votes.

Here is my query, but due to the multiple joining the aggregated values are not the right ones. (At least when there is more than one row of either pictures or votes.)

SELECT    *, SUM( vote_value ) as score, COUNT( picture_id ) AS pictures FROM    gallery LEFT JOIN    vote   ON gallery_id = vote_gallery_ref LEFT JOIN    picture   ON gallery_id = picture_gallery_ref GROUP BY gallery_id 

Because I have noticed that COUNT( DISTINCT picture_id ) gives me the correct number of pictures I tried this:

( SUM( vote_value ) / GREATEST( COUNT( DISTINCT picture_id ), 1 ) ) AS score 

It works in this example, but what if there were more joins in one query?

Just want to know whether there is a better or more ‘elegant’ way this problem can be solved. Also I’d like to know whether my solution is MySQL-specific or standard SQL?

  • 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. 2026-05-11T05:37:45+00:00Added an answer on May 11, 2026 at 5:37 am

    This quote from William of Okham applies here:

    Enita non sunt multiplicanda praeter necessitatem

    (Latin for ‘entities are not to be multiplied beyond necessity’).

    You should reconsider why do you need this to be done in a single query? It’s true that a single query has less overhead than multiple queries, but if the nature of that single query becomes too complex, both for you to develop, and for the RDBMS to execute, then run separate queries.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 106k
  • Answers 106k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Please disregard my previous bogus answer. There is a race… May 11, 2026 at 8:51 pm
  • Editorial Team
    Editorial Team added an answer The first argument of the Field.set method should be the… May 11, 2026 at 8:51 pm
  • Editorial Team
    Editorial Team added an answer Just use globals() globals() — Return a dictionary representing the… May 11, 2026 at 8:51 pm

Related Questions

I am trying to creating an optional association between a couple of tables. I
I have a ASP.Net page using ADO to query MS access database and as
I have a database designed to hold card data from various trading card games.
I have an existing database with the table Transactions in it. I have added

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.