zoukankan      html  css  js  c++  java
  • MySQL学习(七)

    1 最终结果集出来后,可以再排序,反过来说:排序是针对最终结果集,即order by要放在where/group/having后面,顺序不能乱。
    2
    取出第4个栏目下的商品,并按照价格由高到低排序

    mysql> select goods_name,shop_price from goods
        -> where cat_id = 4
        -> order by shop_price;
    

    降序排列

    mysql> select goods_name,shop_price from goods
        -> where cat_id = 4
        -> order by shop_price desc;
    

    asc表示升序排列

    3

    mysql> select goods_name,cat_id,shop_price from goods
        -> order by cat_id;
    

    虽然cat_id按照顺序排列,但是价格不是按照顺序排列。
    要求:按照栏目升序排列,栏目内部价格按照降序排列。
    多列排序

    mysql> select goods_name,cat_id,shop_price from goods
        -> order by cat_id ,shop_price desc;
    

    4
    使用limit限制查询结果得数量

    mysql> select goos_id,cat_id ,goods_name
        -> from goods
        -> where cat_id =3
        -> order by shop_price asc
        -> limit 10;
    

    limit [位置偏移量,] 行数

    mysql> select goos_id,cat_id ,goods_name
        -> from goods
        -> order by shop_price
        -> desc
        -> limit 0,1;
    

    5 练习题
    查询出,每个栏目(cat_id)下id号(goos_id)最大的第一条商品
    典型错误

    mysql> select cat_id,max(goos_id) from goods
        -> group by cat_id;
    

    上面的问题不能用一条SQL语句完成,需要用到子查询

  • 相关阅读:
    if 语句
    变量命名
    python变量
    运算符
    注释
    PyCharm
    python版本,执行
    Tornado 【简述】
    Git 【管理篇】
    MySQL的介绍
  • 原文地址:https://www.cnblogs.com/Manual-Linux/p/10198169.html
Copyright © 2011-2022 走看看