标签:
建表:
1 mysql> create table shop(id int(4) not null auto_increment, 2 -> name varchar(30),price double(15,2),sort varchar(20), 3 -> count int(5) default 0 not null, 4 -> primary key (id,name)); 5 Query OK, 0 rows affected (0.15 sec)
修改表结构:在指定列增加字段。
1 mysql> alter table shop 2 -> add supplier_name varchar(50) not null default ‘tmall‘ 3 -> after price;
输出: 4 Query OK, 5 rows affected (0.35 sec) 5 Records: 5 Duplicates: 0 Warnings: 0
查看表变化: 7 mysql> select * from shop; 8 +----+-------+--------+---------------+------+-------+ 9 | id | name | price | supplier_name | sort | count | 10 +----+-------+--------+---------------+------+-------+ 11 | 1 | apple | 2.50 | tmall | A | 500 | 12 | 2 | BOOK | 2.40 | tmall | B | 500 | 13 | 3 | rule | 1.20 | tmall | B | 200 | 14 | 4 | grape | 8.50 | tmall | A | 500 | 15 | 5 | pen | 108.50 | tmall | B | 80 | 16 +----+-------+--------+---------------+------+-------+ 17 5 rows in set (0.00 sec)
增加记录:
1 mysql> insert into shop values 2 -> (1,‘apple‘,2.50,‘A‘,500), 3 -> (2,‘BOOK‘,2.40,‘B‘,500), 4 -> (3,‘rule‘,1.20,‘B‘,200);
输出: 5 Query OK, 3 rows affected (0.08 sec) 6 Records: 3 Duplicates: 0 Warnings: 0
简单查询语句
1 mysql> select max(price) as price,max(id) as id from shop;
输出:不可以按记录输出... 2 +-------+------+ 3 | price | id | 4 +-------+------+ 5 | 2.50 | 3 | 6 +-------+------+ 7 1 row in set (0.00 sec)
二、输出某列最大值的记录:
方法1:
1 mysql> select * from shop where price =(select max(price) from shop);
输出: 2 +----+-------+-------+------+-------+ 3 | id | name | price | sort | count | 4 +----+-------+-------+------+-------+ 5 | 1 | apple | 2.50 | A | 500 | 6 +----+-------+-------+------+-------+ 7 1 row in set (0.02 sec)
方法2:
1 mysql> select * from shop order by price desc limit 1;
输出: 2 +----+-------+-------+------+-------+ 3 | id | name | price | sort | count | 4 +----+-------+-------+------+-------+ 5 | 1 | apple | 2.50 | A | 500 | 6 +----+-------+-------+------+-------+ 7 1 row in set (0.00 sec)
三、查询组的最大值:查询同类同产品的商品,哪一个商家卖的最高。
1 mysql> select * from shop; 2 +----+-------+--------+---------------+------+-------+ 3 | id | name | price | supplier_name | sort | count | 4 +----+-------+--------+---------------+------+-------+ 5 | 1 | apple | 2.50 | dangdang | A | 500 | 6 | 2 | book | 2.40 | tmall | B | 500 | 7 | 3 | book | 1.20 | jd | B | 200 | 8 | 4 | apple | 8.50 | tmall | A | 500 | 9 | 5 | pen | 108.50 | jd | B | 80 | 10 +----+-------+--------+---------------+------+-------+ 11 5 rows in set (0.00 sec) 12 13 mysql> select supplier_name,sort,name,max(price) as price from shop group by sort,name;
14 输出: 15 +---------------+------+-------+--------+ 16 | supplier_name | sort | name | price | 17 +---------------+------+-------+--------+ 18 | dangdang | A | apple | 8.50 | 19 | tmall | B | book | 2.40 | 20 | jd | B | pen | 108.50 | 21 +---------------+------+-------+--------+ 22 3 rows in set (0.00 sec)
标签:
原文地址:http://www.cnblogs.com/sunshine-habit/p/4816812.html