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
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;
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的主要内容,如果未能解决你的问题,请参考以下文章