zoukankan      html  css  js  c++  java
  • mysql查询表格方法汇总3

    mysql> select gdcode,gdname,gdprice
        -> from goods
        -> where tid=1
        -> order by gdprice;
    
    结果
    +--------+-----------+---------+
    | gdcode | gdname    | gdprice |
    +--------+-----------+---------+
    | 001    | 迷彩帽    |      63 |
    | 008    | A字裙     |     128 |
    | 005    | 运动鞋    |     400 |
    +--------+-----------+---------+
    3 rows in set (0.02 sec)

    mysql> select gdcode,gdname,gdsaleqty,gdprice
        -> from goods
        -> where tid=1
        -> order by gdsaleqty desc,gdprice;

    结果

    +--------+-----------+-----------+---------+
    | gdcode | gdname    | gdsaleqty | gdprice |
    +--------+-----------+-----------+---------+
    | 008    | A字裙     |       200 |     128 |
    | 005    | 运动鞋    |       200 |     400 |
    | 001    | 迷彩帽    |        29 |      63 |
    +--------+-----------+-----------+---------+
    3 rows in set (0.00 sec)

    mysql> select gdcode,gdname,gdprice
        -> from goods
        -> limit 3;
    
    结果
    +--------+--------------+---------+
    | gdcode | gdname       | gdprice |
    +--------+--------------+---------+
    | 001    | 迷彩帽       |      63 |
    | 003    | 牛肉干       |      94 |
    | 004    | 零食礼包     |     145 |
    +--------+--------------+---------+
    3 rows in set (0.00 sec)

    拓展:查询表格从第四行开始,三行数据;

    首先查询表格数据:

    mysql> select gdcode,gdname,gdprice from goods;
    +--------+---------------+---------+
    | gdcode | gdname        | gdprice |
    +--------+---------------+---------+
    | 001    | 迷彩帽        |      63 |
    | 003    | 牛肉干        |      94 |
    | 004    | 零食礼包      |     145 |
    | 005    | 运动鞋        |     400 |
    | 006    | 咖啡壶        |      50 |
    | 008    | A字裙         |     128 |
    | 009    | LED小台灯     |      29 |
    | 010    | 华为P9_PLUS   |    3980 |
    +--------+---------------+---------+
    8 rows in set (0.00 sec)

    查询表格内容,因为第一行是0 行,所以排序是

    0

    1

    2

    3

    ...

    mysql> select gdcode,gdname,gdprice
        -> from goods
        -> limit 4,3;
    +--------+--------------+---------+
    | gdcode | gdname       | gdprice |
    +--------+--------------+---------+
    | 006    | 咖啡壶       |      50 |
    | 008    | A字裙        |     128 |
    | 009    | LED小台灯    |      29 |
    +--------+--------------+---------+
    3 rows in set (0.00 sec)

    mysql> select sum(gdsaleqty) from goods;
    +----------------+
    | sum(gdsaleqty) |
    +----------------+
    |            807 |
    +----------------+
    1 row in set (0.00 sec)
    
    mysql> select max(gdsaleqty) from goods;
    +----------------+
    | max(gdsaleqty) |
    +----------------+
    |            234 |
    +----------------+
    1 row in set (0.00 sec)

    首先创建orders表

    use onlinedb;
    
    SET FOREIGN_KEY_CHECKS=0;
    
    
    -- ----------------------------
    -- Table structure for orders
    -- ----------------------------
    DROP TABLE IF EXISTS `orders`;
    CREATE TABLE `orders` (
     `oID` int(11) NOT NULL AUTO_INCREMENT,
     `uID` int(11) DEFAULT NULL,
     `oTime` datetime NOT NULL,
     `oTotal` float DEFAULT NULL,
     PRIMARY KEY (`oID`)
    ) ENGINE=InnoDB AUTO_INCREMENT=7 DEFAULT CHARSET=utf8;
    
    -- ----------------------------
    
    -- Records of orders
    
    -- ----------------------------
    
    INSERT INTO `orders` VALUES ('1', '1', '2017-12-04 08:45:07', '83');
    INSERT INTO `orders` VALUES ('2', '3', '2017-12-04 08:45:07', '144');
    INSERT INTO `orders` VALUES ('3', '9', '2017-12-04 08:45:07', '29');
    INSERT INTO `orders` VALUES ('4', '8', '2017-12-04 08:45:07', '1049');
    INSERT INTO `orders` VALUES ('5', '4', '2017-12-04 08:45:07', '557');
    INSERT INTO `orders` VALUES ('6', '3', '2017-12-04 08:45:07', '1049');

    直接查询uid是6个人

    mysql> select count(uID) from orders;
    +------------+
    | count(uID) |
    +------------+
    |          6 |
    +------------+
    1 row in set (0.00 sec)
    mysql> select count(distinct uID) from orders;
    
    +---------------------+
    | count(distinct uID) |
    +---------------------+
    |                   5 |
    +---------------------+
    1 row in set (0.00 sec)

    加上distinct是五个人;

    mysql>select uID,uName,uSex,uCity from users group by uCity;

    注意:如果只使用group by只会显示第一个

    解决方法如下

    mysql> select uCity,count(*) from users
        -> group by uCity;

    mysql> select uCity,GROUP_CONCAT(uID)as uIDs
        -> from users
        -> GROUP BY uCity;

    mysql> select uCity,GROUP_CONCAT(uID ORDER BY uID SEPARATOR'_')as '编号'
        -> from users
        -> GROUP BY uCity;

    mysql> select uCity,count(*) from users
        -> where uCity in('长沙','上海')
        -> GROUP BY uCity
        -> WITH ROLLUP;

    mysql> select uCity,count(*) from users
        -> GROUP BY uCity
        -> HAVING COUNT(*)>=3;

    语句中有聚合函数必须用having

    一辈子最少拼到无能为力过一次。
  • 相关阅读:
    Ubuntu 12.04 gedit编辑器 中文乱码
    ubuntu设置vim语法高亮显示和自动缩进
    Linux学习小结(转)
    指向常量的指针和常量指针
    Android之EditText
    android之TextView
    Android存储机制之Preference
    android实现可拖动按钮
    用turtle画图
    torchvision里densenet代码分析
  • 原文地址:https://www.cnblogs.com/yssgxxy/p/12751052.html
Copyright © 2011-2022 走看看