mysql> show tables;
ERROR 1046 (3D000): No database selected
报错:没有数据库被选中
告诉数据库我正在对哪个数据库进行
操作的命令是:
mysql> use myshop1;
Database changed
mysql> show tables;
+-------------------+
| Tables_in_myshop1 |
+-------------------+
| commodity |
| commoditytype |
| customer |
| order |
+-------------------+
4 rows in set (0.09 sec)
最基础的查询语句:
select * from commoditytype;
注:order``Order
MySQL的5.7版本对大小写不敏感
mysql中 使用引号’'来标识值使用反引号``来标识字段或者表名称
数据表的增、删、改、查
我们在添加商品表数据前,先要有它的父表,也就是商品种类表的
数据,这样子表才能正常添加数据!
1.新增数据 使用insert into 关键字
INSERT INTO table_name [(field1,field2...fieldn)] VALUES (value1,value2...valuen);
这里要注意列名要和表中的个列一一对应,顺序名称类型一个都不
能错,后面的数据内容也是一样!(可同时加多个字段,用逗号隔开)
mysql> insert into commoditytype (ct_id,ct_name) values (1,'玩具');
mysql> insert into commoditytype (ct_id,ct_name) values (1,'文具'),(3,'书籍');
ERROR 1062 (23000): Duplicate entry '1' for key 'PRIMARY'
报错:重复添加主键为’1’的记录
mysql> insert into commoditytype (ct_id,ct_name) values (2,'文具'),(3,'书籍');
Query OK, 2 rows affected (0.09 sec)
Records: 2 Duplicates: 0 Warnings: 0
Records:记录 Duplicates:多重记录 Warnings:警告
2.删除表记录,使用DELETE 关键字:
DELETE FROM table_name WHERE CONDITION;
这里千万注意:删除语句一定要写删除条件,否则整张表删除!!!
mysql> delete from commoditytype where ct_id=3;
Query OK, 1 row affected (0.12 sec)
mysql> select * from commoditytype;
+-------+---------+
| ct_id | ct_name |
+-------+---------+
| 1 | 文具 |
| 2 | 文具 |
+-------+---------+
2 rows in set (0.00 sec)
注:未加删除条件语句结果如下
mysql> delete from commoditytype;
Query OK, 2 rows affected (0.12 sec)
mysql> select * from commoditytype;
Empty set (0.00 sec)
3.修改表记录,使用UPDATE 关键字:
UPDATE table_name
SET field1=value1,field2=value2,…fieldn=valuen
WHERE CONDITION;
这里同样注意,一定必须不用忘了条件语句,否则整张表数据将被
修改成一样的!!细思极恐!!!
修改前:
mysql> select * from commoditytype;
+-------+---------+
| ct_id | ct_name |
+-------+---------+
| 1 | 文具 |
| 2 | 文具 |
+-------+---------+
2 rows in set (0.00 sec)
mysql> update commoditytype set ct_name='玩具' where ct_id=2;
Query OK, 1 row affected (0.07 sec)
Rows matched: 1 Changed: 1 Warnings: 0
修改后:
mysql> select * from commoditytype;
+-------+---------+
| ct_id | ct_name |
+-------+---------+
| 1 | 文具 |
| 2 | 玩具 |
+-------+---------+
2 rows in set (0.00 sec)
4.查询语句,使用SELECT 关键字:
SELECT field1,field2, ... ,fieldn FROM table_name;
多个字段用逗号隔开
查询语句如果为空则输出:
Empty set (0.00sec)
MySQL支持最简单的四则运算:
mysql> select c_name as 商品名称,c_outprice as 商品售价,c_inprice as 商品进价,c_outprice-c_inprice as 商品利润 from commodity;
数据库表单查询,使用限制条件,数据库可以包含如下功能:
• 1.带关系运算符和逻辑运算符的条件数据查询;
select c_name,c_inprice from commodity where c_inprice>=10 and c_inprice<=100;
select c_name,c_inprice from commodity where c_inprice<10 or c_inprice>100;
• 2.带BETWEEN AND关键字的条件查询语句;
select c_name,c_inprice from commodity where c_inprice between 10 and 100;
select c_name,c_inprice from commodity where c_inprice not between 10 and 100;
between and 闭区间
not between and 开区间
• 3.带IS NULL关键字的条件查询语句;
select c_name,c_outprice from commodity where c_outprice is null;
select c_name,c_outprice from commodity where c_outprice is not null;
• 4.带IN关键字的条件查询语句;
select c_name,c_outprice from commodity where c_outprice in(10,20,30,40,50);
售价是10或者20或者30…
select c_name,c_outprice from commodity where c_outprice not in(10,20,30,40,50);
售价不是10且不是20且不是30…
in 里面的数字之间是或关系
not in 里面的数字之间是且关系
• 5.带LIKE关键字的条件查询语句;
select c_name from commodity where c_name like '%玩具%';
LIKE要和通配符一起用,不然没有用通配符的LIKE效果等同于“=”
常用通配符有“_”和“%”;前者是匹配单个字符的意思,后者是匹配任意字符;
LIKE关键字在实际开发中,常用于实现模糊查询功能,比如查询张姓同学,
select * from student where stu_name like '张%';
排序的关键字是ORDER BY,后面跟ASC(升序,可以不写默认)或者DESC逆序
SELECT field1,field2,... FROM table_name
WHERE CONDITION
ORDER BY fieldn [ASC|DESC];
可以没有条件语句:
select * from student order by stu_name;
LIMIT关键字,输出我们想要的结果集
select * from table limit 2;
只输出前2行;
select * from table limit 0,5;
从第0行开始,输出5行;
select * from table order by c_outprice limit 1;
按照c_outprice列升序排序,只输出第一行,就得到售价最低的那条记录
所有条件结合查询,将每个关键字语句放置单独一行,where关键字在order之前:
mysql> select c_name,c_outprice
-> from commodity
-> where c_outprice is not null
-> order by c_outprice
-> limit 20;
统计函数(聚合函数)
COUNT()函数:统计记录数;
count(*)使用方式,实现对表中记录进行统计,不管是否包含NULL还是NOT NULL
count(field)使用方法对指定字段进行统计,将忽略NULL值!
如果表中无数据,count()函数返回的是0,其它函数返回null;
AVG()函数:求平均值;
SUM()函数:求和;
MAX()函数:求最大数;
MIN()函数:求最小数;
GROUP BY 关键字实现分组:
SELECT * FROM tabel_name GROUP BY field;
实际工作中分组查询一般都是和统计函数一起出现的:
select count(*) from table group by type;
在mysql中,要对分组查询结果进行条件限制查询,不能使用WHERE关键字,需要使用HAVING关键字
select avg(c_inprice) ,c_type from commodity group by c_type having avg(c_inprice) >1;
注意:having中的字段名不需要出现在结果表中是从5.7版本才开始支持的
distinct一般是用来去除查询结果中的重复记录,只能在SELECT 语句中使用,不能在INSERT,DELETE,UPDATE中使用。具体的语法如下:
select distinct expression[,expression...] from tables [where conditions];
这里的expressions可以是多个字段。