bachx Posted August 21, 2007 Share Posted August 21, 2007 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 https://forums.phpfreaks.com/topic/66073-selecting-the-maximum-value-mysql-query-help/ Share on other sites More sharing options...
trq Posted August 21, 2007 Share Posted August 21, 2007 Providing the name field holds that name your looking for your query looks good. Link to comment https://forums.phpfreaks.com/topic/66073-selecting-the-maximum-value-mysql-query-help/#findComment-330445 Share on other sites More sharing options...
bachx Posted August 21, 2007 Author Share Posted August 21, 2007 Weird. I'm getting the right balance but not the right name. Something must be wrong with that statement. Link to comment https://forums.phpfreaks.com/topic/66073-selecting-the-maximum-value-mysql-query-help/#findComment-330448 Share on other sites More sharing options...
bachx Posted August 22, 2007 Author Share Posted August 22, 2007 Anyone? Link to comment https://forums.phpfreaks.com/topic/66073-selecting-the-maximum-value-mysql-query-help/#findComment-330701 Share on other sites More sharing options...
pranav_kavi Posted August 22, 2007 Share Posted August 22, 2007 try dis, select name from account_info where balance=(select max(balance) from account_info) and address='xxx' Link to comment https://forums.phpfreaks.com/topic/66073-selecting-the-maximum-value-mysql-query-help/#findComment-330712 Share on other sites More sharing options...
bachx Posted August 22, 2007 Author Share Posted August 22, 2007 That returned 0 results. Link to comment https://forums.phpfreaks.com/topic/66073-selecting-the-maximum-value-mysql-query-help/#findComment-330719 Share on other sites More sharing options...
pranav_kavi Posted August 22, 2007 Share Posted August 22, 2007 try tis first... select name,balance from account_info where balance=(select max(balance) from account_info) Link to comment https://forums.phpfreaks.com/topic/66073-selecting-the-maximum-value-mysql-query-help/#findComment-330720 Share on other sites More sharing options...
beboo002 Posted August 22, 2007 Share Posted August 22, 2007 i think there is some problem to group by statement plz drop the group by Link to comment https://forums.phpfreaks.com/topic/66073-selecting-the-maximum-value-mysql-query-help/#findComment-330722 Share on other sites More sharing options...
pranav_kavi Posted August 22, 2007 Share Posted August 22, 2007 r u gettin any output?? Link to comment https://forums.phpfreaks.com/topic/66073-selecting-the-maximum-value-mysql-query-help/#findComment-330726 Share on other sites More sharing options...
bachx Posted August 22, 2007 Author Share Posted August 22, 2007 try tis first... select name,balance from account_info where balance=(select max(balance) from account_info) That worked. But when I try to add another condition, like "AND address='xxx'", it'll return 0 results. Link to comment https://forums.phpfreaks.com/topic/66073-selecting-the-maximum-value-mysql-query-help/#findComment-330727 Share on other sites More sharing options...
pranav_kavi Posted August 22, 2007 Share Posted August 22, 2007 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 https://forums.phpfreaks.com/topic/66073-selecting-the-maximum-value-mysql-query-help/#findComment-330760 Share on other sites More sharing options...
bachx Posted August 22, 2007 Author Share Posted August 22, 2007 Yes, I want to add other conditions. Link to comment https://forums.phpfreaks.com/topic/66073-selecting-the-maximum-value-mysql-query-help/#findComment-330768 Share on other sites More sharing options...
pranav_kavi Posted August 22, 2007 Share Posted August 22, 2007 As only row is being retrieved frm the query...u can do the condition checks on the php side.it wud b much easier. Link to comment https://forums.phpfreaks.com/topic/66073-selecting-the-maximum-value-mysql-query-help/#findComment-330773 Share on other sites More sharing options...
bachx Posted August 22, 2007 Author Share Posted August 22, 2007 I'm sure there is a certain SQL query statement for such a case. Implementing it in php won't be as much effective/fast. Link to comment https://forums.phpfreaks.com/topic/66073-selecting-the-maximum-value-mysql-query-help/#findComment-330792 Share on other sites More sharing options...
pranav_kavi Posted August 22, 2007 Share Posted August 22, 2007 select name from account_info where balance=(select max(balance) from account_info) is the 'address' column retrieved by the abov query 'xxx' ?The abov query only retrieves one row,so if the address condition fails,nothin wud b displayed. Link to comment https://forums.phpfreaks.com/topic/66073-selecting-the-maximum-value-mysql-query-help/#findComment-330805 Share on other sites More sharing options...
bachx Posted August 22, 2007 Author Share Posted August 22, 2007 The address is there, it's not supposed to fail. Anyone? Link to comment https://forums.phpfreaks.com/topic/66073-selecting-the-maximum-value-mysql-query-help/#findComment-330828 Share on other sites More sharing options...
pranav_kavi Posted August 22, 2007 Share Posted August 22, 2007 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 https://forums.phpfreaks.com/topic/66073-selecting-the-maximum-value-mysql-query-help/#findComment-330837 Share on other sites More sharing options...
bachx Posted August 22, 2007 Author Share Posted August 22, 2007 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 https://forums.phpfreaks.com/topic/66073-selecting-the-maximum-value-mysql-query-help/#findComment-330850 Share on other sites More sharing options...
pranav_kavi Posted August 22, 2007 Share Posted August 22, 2007 i think tis s wat u want..... select name,balance from account_info where balance = (select max(balance) from account_info group by address having address='xxx') Link to comment https://forums.phpfreaks.com/topic/66073-selecting-the-maximum-value-mysql-query-help/#findComment-330858 Share on other sites More sharing options...
MadTechie Posted August 22, 2007 Share Posted August 22, 2007 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 Link to comment https://forums.phpfreaks.com/topic/66073-selecting-the-maximum-value-mysql-query-help/#findComment-330876 Share on other sites More sharing options...
bachx Posted August 22, 2007 Author Share Posted August 22, 2007 I'm sure, I've created a sample database and I know the values in it. There are no identical rows/etc. Link to comment https://forums.phpfreaks.com/topic/66073-selecting-the-maximum-value-mysql-query-help/#findComment-331087 Share on other sites More sharing options...
MadTechie Posted August 22, 2007 Share Posted August 22, 2007 does seam weird.. maybe posting a small DB dump Link to comment https://forums.phpfreaks.com/topic/66073-selecting-the-maximum-value-mysql-query-help/#findComment-331115 Share on other sites More sharing options...
bachx Posted August 22, 2007 Author Share Posted August 22, 2007 Anyone else can figure this out? Link to comment https://forums.phpfreaks.com/topic/66073-selecting-the-maximum-value-mysql-query-help/#findComment-331212 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.