MySQL query table method summaryThree

 

MySQL> SELECT gdcode, gdname, gdprice
     -> from Goods
     -> WHERE TID = . 1 
    -> Order by gdprice; 

Results
 + -------- + + --- ----------- ------ + 
| gdcode | gdname | gdprice | 
+ -------- + ----------- + --------- + 
| 001     | Camouflage Cap |       63 | 
| 008     | A-line skirt |      128 | 
| 005     | Sneakers |      400 | 
+ -------- + ----------- + ------- -+ 3 rows in set ( 0.02 sec)
 

 

 

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

result

+ -------- + ----------- + ----------- + --------- + 
| gdcode | gdname | gdsaleqty | gdprice | 
+ -------- + ----------- + ----------- + --------- + 
| 008     | A-line skirt |        200 |      128 | 
| 005     | Sneakers |        200 |      400 | 
| 001     | Camouflage cap |         29 |       63 | 
+ -------- + ----------- + ----------- + --------- + 3 rows in set ( 0.00 sec)
 

MySQL> SELECT gdcode, gdname, gdprice
     -> from Goods
     -> limit . 3 ; 

Results
 + -------- + ------- + -------------- -+ 
| gdcode | gdname | gdprice | 
+ -------- + -------------- + --------- + 
| 001     | Camouflage cap |       63 | 
| 003     | beef jerky |       94 | 
| 004     | snack packs |      145 | 
+ -------- + ------ + -------------- --- + 3 rows in set ( 0.00 sec)
 

Expansion: The query form starts from the fourth row and has three rows of data;

First query the table data:

mysql> select gdcode, gdname, gdprice from goods;
 + -------- + --------------- + --------- + 
| gdcode | gdname | gdprice | 
+ -------- + --------------- + --------- + 
| 001     | Camouflage Hat |       63 | 
| 003     | beef jerky |       94 | 
| 004     | snack packs |      145 | 
| 005     | sneakers |      400 | 
| 006     | coffee maker |       50 | 
| 008     | A word skirt |      128 |
|009     | LED small desk lamp |       29 | 
| 010     | Huawei P9_PLUS |     3980 | 
+ -------- + --------------- + -------- -+ 8 rows in set ( 0.00 sec)
 

 

Query the table content, because the first row is 0 rows, so the sort is

0

1

2

3

...

MySQL> SELECT gdcode, gdname, gdprice
     -> from Goods
     -> limit . 4 , . 3 ;
 + -------- + ------ + -------------- --- + 
| gdcode | gdname | gdprice | 
+ -------- + -------------- + --------- + 
| 006     | Coffee Pot |       50 | 
| 008     | A-line skirt |      128 | 
| 009     | LED small lamp |       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)

 

First create the orders table

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');

Direct inquiry uid is 6 people

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)

Plus distinct is five people;

 

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

Note: If only group by is used, only the first one will be displayed

The solution is as follows

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;

There are aggregate functions in the statement must have

 

 

 

Guess you like

Origin www.cnblogs.com/yssgxxy/p/12751052.html