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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T22:10:18+00:00 2026-06-02T22:10:18+00:00

I have this query working to some extent. It returns the correct value for

  • 0

I have this query working to some extent.
It returns the correct value for ‘rating’ (which output as 7, the highest rating), but the output for ‘content’ is from a different row in the table. (not the row of the highest rating, which is 7)

$bestAnswerQuery = MYSQL_QUERY("SELECT content, MAX(rating) as rating FROM answers WHERE questionID = '$questionID'");

$fetchBestAnswer = MYSQL_FETCH_ASSOC($bestAnswerQuery);

echo "$fetchBestAnswer[content] $fetchBestAnswer[rating]";

Can anyone tell me why? I’ve searched and cannot find out why this isn’t working properly.

  • 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-06-02T22:10:20+00:00Added an answer on June 2, 2026 at 10:10 pm

    This is not how aggregates like MAX work in SQL. Your confusion is coming from MySQL’s (default) non-ANSI handling of aggregates.

    Aggregates like MAX operate over groups. In the absence of a group by clause, the entire result set is considered to be a single group. Only expressions that are part of a group by clause can be included in a select clause without being enclosed in an aggregate. In the case where there is no group by, then all columns or expressions in the select clause must be contained in an aggregate.

    However, MySQL’s default configuration breaks this by allowing you to include non-grouped expressions in the select clause, but the row that any given expression uses to obtain its value is undefined; it could be any row within the group.

    After that long-winded answer, if what you want to get is the maximum rating and the associated content column from the table for a given question, you can just do this:

    select 
        rating, 
        content 
    
    from answers 
    
    where questionID = '$questionID' 
    
    order by rating desc 
    
    limit 1;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this query working but it is not returning back exactly what I
I have this working to a degree, but I am looking for some input
I have this mysql query using PHP and first part is working fine, but
So I'm working on SQL Server 2008 and I have this query which should
I have this login script, but for some reason its not working. I checked
I have this working query: $q = $this->db->query('SELECT u.name FROM users u JOIN user_group
I have written a query like this. It is working and giving me the
I have this query which is running perfectly From this query I am selecting
I have this query in sql server 2000: select pwdencrypt('AAAA') which outputs an encrypted
This query was working for me until recently. I now have 135 InstallationSummary documents

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.