Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

mysql max

Status
Not open for further replies.

Laeg

Programmer
Nov 29, 2004
95
IE
tblTest
=============
a | b
=============
1 | 1
1 | 2

select a, max(b) from tblTest
group by b

returns both records

select a, max(b) from tblTest
group by a

returns a single record

How does max work in MySQL???
 
That's exactly how it's supposed to work in standard SQL. If you group by A, then you get a record for each different value of A. If you group by B, you get a record for each different value of B. Try it with a bigger set of records and values and it should become clearer.
 
How does max work in MySQL???
exactly the same as it does in every other database system :)


what's different in mysql is the way GROUP BY works

in any other database system, this is invalid and will give you a syntax error --

select a, max(b) from tblTest
group by b

the reason it's invalid is because there is a column in the SELECT clause which is not an aggregate expression but isn't mentioned in the GROUP BY clause

for more on how GROUP BY works, please read the following two pages:

* GROUP BY and HAVING with Hidden Fields

* Debunking GROUP BY Myths




r937.com | rudy.ca
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top