MySQL选择字段中值最大的行

时间:2014-02-26 20:45:24

标签: mysql

我正在为我的软件制作销售组件,系统允许以多种付款方式支付产品。因此,如果我引入销售代码,系统将显示该人的姓名,产品,支付的金额以及付款的数量。

我有这个问题:

select cliente.Ape_Pat, 
    cliente.Ape_Mat, 
    cliente.Nom, 
    venta.Total, 
    venta.Pagado, 
    concat(evento.Nombre, ' ' ,evento.Fecha), 
    venta.ndp 
from venta, cliente, evento 
where venta.folio = '123456' 
  and venta.Id_cliente = cliente.Id_cliente 
  and venta.Id_evento = evento.Id_evento;

问题是此查询显示所有结果

Ape_Pat  Ape_Mat Nom      Total  Pagado      Concat            NDP 
Torres   Cuevas  Gustavo | 2376 | 2370 | Lamp July 2nd  |   1
Torres   Cuevas  Gustavo | 2376 | 2371 | Lamp July 3rd  |   2
Torres   Cuevas  Gustavo | 2376 | 2372 | Lamp July 4th  |   3

NDP是最后一笔付款的编号,我如何才能获取NDP值最高的行?

3 个答案:

答案 0 :(得分:3)

使用ORDER DESCLIMIT:您应该在查询结尾处添加:

ORDER BY NDP DESC 
LIMIT 0 , 1

答案 1 :(得分:1)

试试这个:

 select cliente.Ape_Pat, cliente.Ape_Mat, cliente.Nom, venta.Total, venta.Pagado,
 concat(evento.Nombre, ' ' ,evento.Fecha), venta.ndp from venta, cliente, 
 evento where venta.folio = '123456' 
 and venta.Id_cliente = cliente.Id_cliente 
 and venta.Id_evento = evento.Id_evento
 and venta.npd = (select max(npd) from venta where venta.folio = '123456');

答案 2 :(得分:0)

添加ORDER BY和LIMIT子句。即SELECT ... FROM ... WHERE ... ORDER BY venta.NDP DESC LIMIT 0,1