可是我不会……mysql的sql脚本:CREATE TABLE `test`.`order_info` (
`order_Id` INT( 5 ) NOT NULL ,
`order_date` DATE NOT NULL ,
`total_price` INT( 8 ) NOT NULL ,
`customer` VARCHAR( 50 ) NOT NULL
)INSERT INTO `order_info` (`order_Id`, `order_date`, `total_price`, `customer`) VALUES
(1, '2009-01-05', 524, 'zhangsan'),
(2, '2006-01-15', 3564, 'lisi'),
(3, '2006-12-13', 2314, 'wangwu'),
(4, '2008-05-13', 32514, 'lisi'),
(5, '2003-11-25', 4514, 'zhangsan'),
(6, '2000-09-03', 457, 'wangwu');
很简单的一个订单表,我想提取出每个客户最贵的订单信息--要求结果中要包含订单的日期以及订单号,不仅仅是客户姓名与最贵的价钱是多少熟悉的解答下吧
谢谢!

解决方案 »

  1.   

    select * from `order_info` a
    where not exists (select 1 from `order_info` b where b.customer=a.customer and b.total_price>a.total_price);
      

  2.   

    或:
    select a.* from `order_info` a inner join (select customer,max(total_price) as max_price from `order_info` group by customer) b on a.customer=b.customer and a.total_price=b.max_price);
      

  3.   

    mysql> select * from order_info;
    +----------+------------+-------------+----------+
    | order_Id | order_date | total_price | customer |
    +----------+------------+-------------+----------+
    |        1 | 2009-01-05 |         524 | zhangsan |
    |        2 | 2006-01-15 |        3564 | lisi     |
    |        3 | 2006-12-13 |        2314 | wangwu   |
    |        4 | 2008-05-13 |       32514 | lisi     |
    |        5 | 2003-11-25 |        4514 | zhangsan |
    |        6 | 2000-09-03 |         457 | wangwu   |
    +----------+------------+-------------+----------+
    6 rows in set (0.00 sec)mysql> select *
        -> from order_info a
        -> where not exists (select 1 from order_info where customer=a.customer and
    total_price>a.total_price);
    +----------+------------+-------------+----------+
    | order_Id | order_date | total_price | customer |
    +----------+------------+-------------+----------+
    |        3 | 2006-12-13 |        2314 | wangwu   |
    |        4 | 2008-05-13 |       32514 | lisi     |
    |        5 | 2003-11-25 |        4514 | zhangsan |
    +----------+------------+-------------+----------+
    3 rows in set (0.06 sec)mysql>
      

  4.   

    支持分组的写法,可读性更好。
    select a.* 
    from `order_info` a 
    inner join (select customer,max(total_price) as max_price from `order_info` group by customer) b on a.customer=b.customer and a.total_price=b.max_price;
      

  5.   

    join的写法不错,速度快,理解起来也容易not exist的写法速度慢些不过今天我提到的这种情况相对简单些,没有where牵涉在其中我实际中遇到的情况是有where的,join的写法where一定要在内外查询中都限定好where条件,才可exist的写法中,加上where就更难读懂了这是我的看法谢谢各位!