![]() | |
![]() |
| | Thread Tools | Display Modes |
#1
| |||
| |||
|
|
Field | Type | Null | Key | Default | Extra | +--------+---------+------+-----+---------+----------------+ Key | int(11) | NO | PRI | NULL | auto_increment | Value1 | int(11) | NO | | NULL | | Value2 | int(11) | NO | | NULL | | +--------+---------+------+-----+---------+----------------+ |
|
Key | Value1 | Value2 | +-----+--------+--------+ 1 | 1 | 2 | 2 | 1 | 3 | 3 | 1 | 4 | 4 | 10 | 8 | 5 | 10 | 2 | 6 | 10 | 12 | +-----+--------+--------+ |
|
Value1 | MAX(Value2) | +--------+-------------+ 1 | 4 | <== Key = 3, how can I determine this? 10 | 12 | <== Key = 6, how can I determine this? +--------+-------------+ |
#2
| |||
| |||
|
|
I am doing a GROUP BY to determine the MAX value in a GROUP. How can I determine, with a query, the primary key of each row that has the MAX value? Please see example below. Thank you, Joe mysql> describe MyTable; +--------+---------+------+-----+---------+----------------+ | Field | Type | Null | Key | Default | Extra | +--------+---------+------+-----+---------+----------------+ | Key | int(11) | NO | PRI | NULL | auto_increment | | Value1 | int(11) | NO | | NULL | | | Value2 | int(11) | NO | | NULL | | +--------+---------+------+-----+---------+----------------+ mysql> select * from MyTable; +-----+--------+--------+ | Key | Value1 | Value2 | +-----+--------+--------+ | 1 | 1 | 2 | | 2 | 1 | 3 | | 3 | 1 | 4 | | 4 | 10 | 8 | | 5 | 10 | 2 | | 6 | 10 | 12 | +-----+--------+--------+ mysql> select Value1, MAX(Value2) from MyTable Group BY Value1; +--------+-------------+ | Value1 | MAX(Value2) | +--------+-------------+ | 1 | 4 |<== Key = 3, how can I determine this? | 10 | 12 |<== Key = 6, how can I determine this? +--------+-------------+ |
#3
| |||
| |||
|
|
I am doing a GROUP BY to determine the MAX value in a GROUP. How can I determine, with a query, the primary key of each row that has the MAX value? |
![]() |
| Thread Tools | |
| Display Modes | |
| |