`
david_je
  • 浏览: 373175 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

max函数

阅读更多
创建表
mysql> CREATE TABLE shop (
    -> article INT(4) UNSIGNED ZEROFILL DEFAULT '0000' NOT NULL,
    -> dealer  CHAR(20)                 DEFAULT ''     NOT NULL,
    -> price   DOUBLE(16,2)             DEFAULT '0.00' NOT NULL,
    -> PRIMARY KEY(article, dealer));

列的最大值
SELECT MAX(article) AS article FROM shop;
找出最贵物品的编号、销售商和价格。
SELECT article, dealer, price
FROM   shop
WHERE  price=(SELECT MAX(price) FROM shop);

或者
SELECT article, dealer, price 
FROM shop
ORDER BY price DESC
LIMIT 1;

任务:每项物品的的最高价格是多少?
SELECT article, MAX(price) AS price
FROM   shop
GROUP BY article

对每项物品,找出最贵价格的物品的经销商。
ELECT article, dealer, price
FROM   shop s1
WHERE  price=(SELECT MAX(s2.price)
              FROM shop s2
              WHERE s1.article = s2.article);
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics