mysql查询表格方法汇总3

Posted yssgxxy

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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

 

 

 

技术图片

以上是关于mysql查询表格方法汇总3的主要内容,如果未能解决你的问题,请参考以下文章

mysql查询表格方法汇总1

如何利用ClickHouse高速汇总查询MySQL数据

如何在 MySQL 中汇总表以获得基于几列的 3 个月平均收入?

在 Google 表格中整理/汇总特定数据的有效方法

在MySql中、怎样根据年份或者月份查询数据表中的数据?

MyBatis查询mysql数据返回null