假设有这样的两个数据库以及库中的表:
1、客户信息库user
  该数据库有客户登记表register
  表register中有数据
  uid  name  phone
------------------------------------------------
  1   zhang  13123456789
  2   li    13234567890
  3   wang  13345678901
  4   ma   134567890122、产品库product
  产品销售表sell
  表sell中有数据
  uid  barcode    price
------------------------------------------------
  4   092636248246 200.00
  3   6921316905541 5.50
  3   6911989251236 3.50
  1   9787810776462 33.00
  4   6911352003370 13.80问题:
我想从这两个表中取出每个客户购买过的价格最高的商品,期望的结果为
  name  barcode     price
------------------------------------------------
  zhang  9787810776462  33.00
  wang  6921316905541   5.50
  ma   092636248246   200.00product.sell表要按用户id分组,也要排序,后与user.register内联
不知道应该用SQL语句如何实现?

解决方案 »

  1.   

    select * from registe a inner join product b on a.uid=b.uid
    where not exists(select 1 from product where uid=b.uid and price>b.price)
      

  2.   

    select *
    from user u inner join sell s using(uid)
    where not exists (select 1 from sell where uid=s.uid and price>s.price)
      

  3.   

    or
    select a.name,b.barcode,b.price from registe a inner join product b on a.uid=b.uid 
    and a.price<=b.price
    group by a.name,b.barcode,b.price
    having count(b.*)=1
      

  4.   

    谢谢4wb和ACMAIN_CHM,我验证一下,通过后给分。
      

  5.   

    4wb你好,我试了一下你在1楼的
    select * from register a inner join product b on a.uid=b.uid
    where not exists(select 1 from product where uid=b.uid and price>b.price)好象要“数据库.表名”这样的表达才通过,否则报错:
    SELECT * FROM user.register a INNER JOIN product.sell b ON a.uid=b.uid
    WHERE not exists(SELECT 1 FROM product.sell WHERE uid=b.uid AND price>b.price)但是上面的表达,每个单独的uid都返回两个结果,我只想返回一个。
      

  6.   

    mysql> select * from user;
    +------+-------+-------------+
    | uid  | name  | phone       |
    +------+-------+-------------+
    |    1 | zhang | 13123456789 |
    |    2 | li    | 13234567890 |
    |    3 | wang  | 13345678901 |
    |    4 | ma    | 13456789012 |
    +------+-------+-------------+
    4 rows in set (0.00 sec)mysql> select * from sell;
    +------+---------------+--------+
    | uid  | barcode       | price  |
    +------+---------------+--------+
    |    4 | 092636248246  | 200.00 |
    |    3 | 6921316905541 |   5.50 |
    |    3 | 6911989251236 |   3.50 |
    |    1 | 9787810776462 |  33.00 |
    |    4 | 6911352003370 |  13.80 |
    +------+---------------+--------+
    5 rows in set (0.00 sec)mysql> select *
        -> from user u inner join sell s using(uid)
        -> where not exists (select 1 from sell where uid=s.uid and price>s.price);
    +------+-------+-------------+---------------+--------+
    | uid  | name  | phone       | barcode       | price  |
    +------+-------+-------------+---------------+--------+
    |    4 | ma    | 13456789012 | 092636248246  | 200.00 |
    |    3 | wang  | 13345678901 | 6921316905541 |   5.50 |
    |    1 | zhang | 13123456789 | 9787810776462 |  33.00 |
    +------+-------+-------------+---------------+--------+
    3 rows in set (0.06 sec)mysql>
      

  7.   

    谢谢ACMAIN_CHM的实际验证请问如何在mysql>下验证对两个数据库中两个表的查询?我刚刚学习SQL
      

  8.   

    分已经给了,不过问题还未解决。因为涉及的是两个数据库的表:
    user.register
    product.sell我最初的想法是对product.sell表先按照price进行排序(ORDER BY),后按uid进行分组(GROUP BY),最后和user.register联接(JOIN)。
    问题是GROUP BY好像不允许在ORDER BY之后进行,可GROUP BY出来的只有一个结果,它并不是price最大那个。