Jump to content

Selecting the maximum value (MySQL query help)


bachx

Recommended Posts

Alright, I have a quick and probably an easy question. I want to select the maximum balance in my account_info table, along with the name of the account with the highest balance. The statement goes like this:

 

$max_query = mysql_query("SELECT name, max(balance) FROM account_info WHERE address='xxx' GROUP BY name LIMIT 1")

 

The problem is, while it returns the maximum balance, it does not return the proper name for that balance. How can I make it so that it returns the maximum balance along with the matching account name?

 

Cheers.

Link to comment
Share on other sites

As per ur inital post,

I want to select the maximum balance in my account_info table, along with the name of the account with the highest balance.

the query in the last post achieves the above objective.Why do u need 'AND address='xxx''?

i assume u want to display the name wid highest salary only if his address is 'xxx'..rite?

 

Link to comment
Share on other sites

select name,balance from emp where balance=(select max(balance) from emp) and address='dxb'

 

i had tried the above query on one of my tables..it displayed one record with name,balance only bcos the address column had 'dxb' value.

did u try,

select name,balance from emp where balance=(select max(balance) from emp) and address like '%xxx%'

Link to comment
Share on other sites

I figured why it was not returning any results. Your query checks if the maximum balance (for all records) is found within that address, and because it's not, it wasn't returning any values.

 

That's not what I want, I want to find out the maximum balance for a certain address, not for all the records. Hope I made it clear.

 

It should be something similar to my original statement:

SELECT name, max(balance) FROM account_info WHERE address='xxx' GROUP BY name LIMIT 1

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.