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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T01:37:02+00:00 2026-05-14T01:37:02+00:00

I am using MySQL and PHP to build a data layer for a flash

  • 0

I am using MySQL and PHP to build a data layer for a flash game. Retrieving lists of levels is pretty easy, but I’ve hit a roadblock in trying to fetch the level’s average rating along with it’s pointer information. Here is an example data set:

levels Table:

level_id | level_name
       1 | Some Level
       2 | Second Level
       3 | Third Level

ratings Table:

rating_id | level_id | rating_value
        1 |    1     |      3
        2 |    1     |      4
        3 |    1     |      1
        4 |    2     |      3
        5 |    2     |      4
        6 |    2     |      1
        7 |    3     |      3
        8 |    3     |      4
        9 |    3     |      1

I know this requires a join, but I cannot figure out how to get the average rating value based on the level_id when I request a list of levels. This is what I’m trying to do:

SELECT levels.level_id, AVG(ratings.level_rating WHERE levels.level_id = ratings.level_id) FROM levels

I know my SQL is flawed there, but I can’t figure out how to get this concept across. The only thing I can get to work is returning a single average from the entire ratings table, which is not very useful.

Ideal Output from the above conceptually valid but syntactically awry query would be:

level_id | level_rating
        1| 3.34
        2| 1.00
        3| 4.54 

My main issue is I can’t figure out how to use the level_id of each response row before the query has been returned. It’s like I want to use a placeholder… or an alias… I really don’t know and it’s very frustrating. The solution I have in place now is an EPIC band-aid and will only cause me problems long term… please help!

  • 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-14T01:37:02+00:00Added an answer on May 14, 2026 at 1:37 am

    If your levels table is InnoDB:

    SELECT  l.level_id, COALESCE(AVG(level_rating), 0)
    FROM    levels l
    LEFT JOIN
            ratings r
    GROUP BY
            l.id
    

    If your levels table is MyISAM:

    SELECT  l.level_id,
            (
            SELECT  COALESCE(AVG(level_rating), 0)
            FROM    rating r
            WHERE   r.level_id = l.level_id
            )
    FROM    levels l
    

    Subquery is more efficient for MyISAM, since it’s heap-based and GROUP BY would require sorting and/or materialization.

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

Sidebar

Related Questions

I am using mysql with php Yii framework.When doing data modelling I came accross
I'm using PHP, jQuery and mySQL to build an admin site to my webpage.
I want to build a chat system in nodeJs + MYSQL using php. It
I'm using PHP to extract data from a MySQL database. I am able to
(Using MySQL and PHP) I have a search form that will allow my users
I'm using MySQL with php. Through a php script i'm trying to import a
I am using MYSQL and PHP. I have a table called item. Two of
I'm using MySQL with PHP. This is like my table: (I'm using 3 values,
Using PHP / MySQL all encoded up as UTF, we have recently had to
Im using MySQL with PDO PHP scripts to maintain a user database and a

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.