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.

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?

 

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%'

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

Weird. I'm getting the right balance but not the right name. Something must be wrong with that statement.

 

I have to ask but, how do you know that? as you sure the file is "name" ?

 

EDIT: its possible theirs 2 rows with the same balance

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.