anon0
June 7th, 2012, 01:08 PM
I have a table 'mytable' with two columns, 'name' and 'value'.
I want to select all 'name' of rows whose value = max(value)
If I do it without a nested select, it works correctly:
SELECT MAX(value) FROM mytable
store this value in PHP as $max_value
then do,
SELECT name FROM mytable WHERE value = '$max_value'
But when I tried to do a nested select, it failed. I've tried:
SELECT name
FROM mytable
WHERE value = (SELECT MAX(value) FROM mytable);
SELECT name
FROM myTable
WHERE value IN (SELECT max(value) FROM myTable)
Both failed to retrieve any rows, even though some suggested that the syntax seem ok at a glance. The MySQL version is 5.1.58-1ubuntu1.
Any ideas?
I want to select all 'name' of rows whose value = max(value)
If I do it without a nested select, it works correctly:
SELECT MAX(value) FROM mytable
store this value in PHP as $max_value
then do,
SELECT name FROM mytable WHERE value = '$max_value'
But when I tried to do a nested select, it failed. I've tried:
SELECT name
FROM mytable
WHERE value = (SELECT MAX(value) FROM mytable);
SELECT name
FROM myTable
WHERE value IN (SELECT max(value) FROM myTable)
Both failed to retrieve any rows, even though some suggested that the syntax seem ok at a glance. The MySQL version is 5.1.58-1ubuntu1.
Any ideas?