码迷,mamicode.com
首页 > 数据库 > 详细

mysql查询表格方法汇总3

时间:2020-04-22 13:05:08      阅读:82      评论:0      收藏:0      [点我收藏+]

标签:sts   span   sep   技术   一个   长沙   运动   rom   ice   

技术图片

 

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

标签:sts   span   sep   技术   一个   长沙   运动   rom   ice   

原文地址:https://www.cnblogs.com/yssgxxy/p/12751052.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!