MySQL SQL Query Help
am 13.11.2006 23:58:30 von Peter Beckman
I have a table:
id fkid foo bar
1 1 345 yellow
2 1 34 red
3 2 3459 green
4 2 345 brown
I want to select the largest value of "foo" for a unique fkid, and return
bar, the results ordered by bar. In this case, 345 is the largest value of
foo for fkid 1, and 3459 is the largest for fkid 2. "green" comes before
"yellow." My desired result set would be:
fkid foo bar
2 3459 green
1 345 yellow
How would I write that in SQL? fkid and foo are ints, bar is a varchar.
Beckman
------------------------------------------------------------ ---------------
Peter Beckman Internet Guy
beckman@purplecow.com http://www.purplecow.com/
------------------------------------------------------------ ---------------
--
PHP Database Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
RE: MySQL SQL Query Help
am 14.11.2006 00:13:15 von MIGUEL ANTONIO GUIRAO AGUILAR
select max(bar) from mytable where unique fkid order by bar asc
as far as I remember!!
-----Original Message-----
From: Peter Beckman [mailto:beckman@purplecow.com]
Sent: Lunes, 13 de Noviembre de 2006 04:59 p.m.
To: PHP-DB Mailing List
Subject: [PHP-DB] MySQL SQL Query Help
I have a table:
id fkid foo bar
1 1 345 yellow
2 1 34 red
3 2 3459 green
4 2 345 brown
I want to select the largest value of "foo" for a unique fkid, and return
bar, the results ordered by bar. In this case, 345 is the largest value of
foo for fkid 1, and 3459 is the largest for fkid 2. "green" comes before
"yellow." My desired result set would be:
fkid foo bar
2 3459 green
1 345 yellow
How would I write that in SQL? fkid and foo are ints, bar is a varchar.
Beckman
------------------------------------------------------------ ---------------
Peter Beckman Internet Guy
beckman@purplecow.com http://www.purplecow.com/
------------------------------------------------------------ ---------------
--
PHP Database Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Este mensaje es exclusivamente para el uso de la persona o entidad a quien esta dirigido; contiene informacion estrictamente confidencial y legalmente protegida, cuya divulgacion es sancionada por la ley. Si el lector de este mensaje no es a quien esta dirigido, ni se trata del empleado o agente responsable de esta informacion, se le notifica por medio del presente, que su reproduccion y distribucion, esta estrictamente prohibida. Si Usted recibio este comunicado por error, favor de notificarlo inmediatamente al remitente y destruir el mensaje. Todas las opiniones contenidas en este mail son propias del autor del mensaje y no necesariamente coinciden con las de Radiomovil Dipsa, S.A. de C.V. o alguna de sus empresas controladas, controladoras, afiliadas y subsidiarias. Este mensaje intencionalmente no contiene acentos.
This message is for the sole use of the person or entity to whom it is being sent. Therefore, it contains strictly confidential and legally protected material whose disclosure is subject to penalty by law. If the person reading this message is not the one to whom it is being sent and/or is not an employee or the responsible agent for this information, this person is herein notified that any unauthorized dissemination, distribution or copying of the materials included in this facsimile is strictly prohibited. If you received this document by mistake please notify immediately to the subscriber and destroy the message. Any opinions contained in this e-mail are those of the author of the message and do not necessarily coincide with those of Radiomovil Dipsa, S.A. de C.V. or any of its control, controlled, affiliates and subsidiaries companies. No part of this message or attachments may b
e used or reproduced in any manner whatsoever.
--
PHP Database Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Re: MySQL SQL Query Help
am 14.11.2006 00:22:11 von Niel Archer
Hi
Try:
SELECT fkid, MAX(foo), bar FROM table GROUP BY fkid ORDER BY bar DESC
Niel
--
PHP Database Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Re: MySQL SQL Query Help
am 14.11.2006 01:21:13 von bluefx13
Actually, that should not work, it should give you an error.
This should work:
SELECT `fkid`,max(`foo`) as foo,`bar` FROM `test2` GROUP BY `fkid` ORDER BY `bar` ASC
Miguel Guirao wrote:
> select max(bar) from mytable where unique fkid order by bar asc
>
> as far as I remember!!
>
> -----Original Message-----
> From: Peter Beckman [mailto:beckman@purplecow.com]
> Sent: Lunes, 13 de Noviembre de 2006 04:59 p.m.
> To: PHP-DB Mailing List
> Subject: [PHP-DB] MySQL SQL Query Help
>
>
> I have a table:
>
> id fkid foo bar
> 1 1 345 yellow
> 2 1 34 red
> 3 2 3459 green
> 4 2 345 brown
>
> I want to select the largest value of "foo" for a unique fkid, and return
> bar, the results ordered by bar. In this case, 345 is the largest value of
> foo for fkid 1, and 3459 is the largest for fkid 2. "green" comes before
> "yellow." My desired result set would be:
>
> fkid foo bar
> 2 3459 green
> 1 345 yellow
>
> How would I write that in SQL? fkid and foo are ints, bar is a varchar.
>
> Beckman
> ------------------------------------------------------------ ---------------
> Peter Beckman Internet Guy
> beckman@purplecow.com http://www.purplecow.com/
> ------------------------------------------------------------ ---------------
>
>
--
PHP Database Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Re: MySQL SQL Query Help
am 14.11.2006 04:24:33 von Peter Beckman
On Mon, 13 Nov 2006, bluefx13@gmail.com wrote:
> Actually, that should not work, it should give you an error.
>
> This should work:
>
> SELECT `fkid`,max(`foo`) as foo,`bar` FROM `test2` GROUP BY `fkid` ORDER BY `bar` ASC
Yes, but if the data is in a different order that fails and doesn't maintain row order:
mysql> create temporary table test2 (id tinyint,fkid tinyint, foo smallint, bar varchar(20));
mysql> insert into test2 values (1,1,34,'red'), (2,1,345,'yellow'), (3,2,345,'brown'), (4,2,3459,'green');
mysql> select * from test2;
+------+------+------+--------+
| id | fkid | foo | bar |
+------+------+------+--------+
| 1 | 1 | 34 | red |
| 2 | 1 | 345 | yellow |
| 3 | 2 | 345 | brown |
| 4 | 2 | 3459 | green |
+------+------+------+--------+
mysql> SELECT `fkid`,max(`foo`) as foo,`bar` FROM `test2` GROUP BY `fkid` ORDER BY `bar` ASC;
+------+------+-------+
| fkid | foo | bar |
+------+------+-------+
| 2 | 3459 | brown |
| 1 | 345 | red |
+------+------+-------+
2 rows in set (0.00 sec)
Notice how 3459 is supposed to be green but reports brown, and 345 should
be yellow but reports red?
Any other solutions that maintain row integrity?
Beckman
> Miguel Guirao wrote:
>> select max(bar) from mytable where unique fkid order by bar asc
>>
>> as far as I remember!!
>>
>> -----Original Message-----
>> From: Peter Beckman [mailto:beckman@purplecow.com]
>> Sent: Lunes, 13 de Noviembre de 2006 04:59 p.m.
>> To: PHP-DB Mailing List
>> Subject: [PHP-DB] MySQL SQL Query Help
>>
>>
>> I have a table:
>>
>> id fkid foo bar
>> 1 1 345 yellow
>> 2 1 34 red
>> 3 2 3459 green
>> 4 2 345 brown
>>
>> I want to select the largest value of "foo" for a unique fkid, and return
>> bar, the results ordered by bar. In this case, 345 is the largest value of
>> foo for fkid 1, and 3459 is the largest for fkid 2. "green" comes before
>> "yellow." My desired result set would be:
>>
>> fkid foo bar
>> 2 3459 green
>> 1 345 yellow
>>
>> How would I write that in SQL? fkid and foo are ints, bar is a varchar.
>>
>> Beckman
>> ------------------------------------------------------------ ---------------
>> Peter Beckman Internet Guy
>> beckman@purplecow.com http://www.purplecow.com/
>> ------------------------------------------------------------ ---------------
>>
>>
>
> --
> PHP Database Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
>
------------------------------------------------------------ ---------------
Peter Beckman Internet Guy
beckman@purplecow.com http://www.purplecow.com/
------------------------------------------------------------ ---------------
--
PHP Database Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Re: MySQL SQL Query Help
am 14.11.2006 04:51:33 von Chris
Peter Beckman wrote:
> On Mon, 13 Nov 2006, bluefx13@gmail.com wrote:
>
>> Actually, that should not work, it should give you an error.
>>
>> This should work:
>>
>> SELECT `fkid`,max(`foo`) as foo,`bar` FROM `test2` GROUP BY `fkid`
>> ORDER BY `bar` ASC
>
> Yes, but if the data is in a different order that fails and doesn't
> maintain row order:
>
> mysql> create temporary table test2 (id tinyint,fkid tinyint, foo
> smallint, bar varchar(20));
> mysql> insert into test2 values (1,1,34,'red'), (2,1,345,'yellow'),
> (3,2,345,'brown'), (4,2,3459,'green');
> mysql> select * from test2;
> +------+------+------+--------+
> | id | fkid | foo | bar |
> +------+------+------+--------+
> | 1 | 1 | 34 | red |
> | 2 | 1 | 345 | yellow |
> | 3 | 2 | 345 | brown |
> | 4 | 2 | 3459 | green |
> +------+------+------+--------+
> mysql> SELECT `fkid`,max(`foo`) as foo,`bar` FROM `test2` GROUP BY
> `fkid` ORDER BY `bar` ASC;
> +------+------+-------+
> | fkid | foo | bar |
> +------+------+-------+
> | 2 | 3459 | brown |
> | 1 | 345 | red |
> +------+------+-------+
> 2 rows in set (0.00 sec)
>
> Notice how 3459 is supposed to be green but reports brown, and 345 should
> be yellow but reports red?
>
> Any other solutions that maintain row integrity?
You might have to go a subquery.
(I'm not great at subqueries so there would have to be a better way to
write this anyway).
This works in postgresql:
select fkid, foo, bar from test2 t2 where (select max(foo) from test2 t1
where t1.fkid=t2.fkid)=foo;
but mysql won't let you reference the same table inside & outside:
mysql> select fkid, foo, bar from test2 t2 where (select max(foo) from
test2 t1 where t1.fkid=t2.fkid)=foo;
ERROR 1137 (HY000): Can't reopen table: 't2'
http://dev.mysql.com/doc/refman/5.0/en/subqueries.html
The mysql mailing list might have better ideas..
--
Postgresql & php tutorials
http://www.designmagick.com/
--
PHP Database Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php