必须保证所有的()、单引号、双引号是成对结束的 |
---|
必须使用英文状态下的半角输入方式 |
字符串型和日期时间类型的数据可以使用单引号(' ')表示 |
列的别名,尽量使用双引号(" "),而且不建议省略as |
单行注释 | #注释文字(MySQL特有的方式) |
---|---|
单行注释 | -- 注释文字(--后面必须包含一个空格。) |
多行注释 | /* 注释文字 */ |
数据库、表名不得超过30个字符,变量名限制为29个 |
---|
数据库、表名不得超过30个字符,变量名限制为29个 |
数据库名、表名、字段名等对象名中间不要包含空格 |
同一个MySQL软件中,数据库不能同名;同一个库中,表不能重名;同一个表中,字段不能重名 |
必须保证你的字段没有和保留字、数据库系统或常用方法冲突。如果坚持使用,请在SQL语句中使 用`(着重号)引起来 |
保持字段名和类型的一致性,在命名字段并为其指定数据类型的时候一定要保证一致性。假如数据 类型在一个表里是整数,那在另一个表里可就别变成字符型了 |
show databases;
SHOW DATABASES;
create table student info(...);
#表名错误,因为表名有空格 create table student_info(...);
CREATE TABLE `order`(
id INT,
lname VARCHAR(20)
);
select id as "编号", `name` as "姓名" from t_stu; #起别名时,as都可以省略
select id as 编号, `name` as 姓名 from t_stu; #如果字段别名中没有空格,那么可以省略""
select id as 编 号, `name` as 姓 名 from t_stu; #错误,如果字段别名中有空格,那么不能省
略""
在命令行客户端登录mysql,使用source指令导入
mysql> source d:\mysqldb.sql
mysql> desc employees;
+----------------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+----------------+-------------+------+-----+---------+-------+
| employee_id | int(6) | NO | PRI | 0 | |
| first_name | varchar(20) | YES | | NULL | |
| last_name | varchar(25) | NO | | NULL | |
| email | varchar(25) | NO | UNI | NULL | |
| phone_number | varchar(20) | YES | | NULL | |
| hire_date | date | NO | | NULL | |
| job_id | varchar(10) | NO | MUL | NULL | |
| salary | double(8,2) | YES | | NULL | |
| commission_pct | double(2,2) | YES | | NULL | |
| manager_id | int(6) | YES | MUL | NULL | |
| department_id | int(4) | YES | MUL | NULL | |
+----------------+-------------+------+-----+---------+-------+
11 rows in set (0.00 sec)
select
[all|distinct]
<目标列的表达式1> [别名],
<目标列的表达式2> [别名]...
from <表名或视图名> [别名],<表名或视图名> [别名]...
[where<条件表达式>]
[group by <列名>
[having <条件表达式>]]
[order by <列名> [asc|desc]]
[limit <数字或者列表>];
select *| 列名 from 表 where 条件
create database if not exist mydb2;
use mydb2;
create table product(
pid int primary key auto_increment, # 商品编号
pname varchar(20) not null , -# 商品名字
price double, # 商品价格
category_id varchar(20) # 商品所属分类
);
insert into product values(null,'海尔洗衣机',5000,'c001');
insert into product values(null,'美的冰箱',3000,'c001');
insert into product values(null,'格力空调',5000,'c001');
insert into product values(null,'九阳电饭煲’,200,'c001');
insert into product values(null,'啄木鸟衬衣',300,'c002');
insert into product values(null,'恒源祥西裤',800,'c002');
insert into product values(null,'花花公子夹克',440,'c002');
insert into product values(null,'劲霸休闲裤',266,'c002');
insert into product values(null,'海澜之家卫衣',180,'c002');
insert into product values(null,'杰克琼斯运动裤',430,'c002');
insert into product values(null,'兰蔻面霜',300,'c003');
insert into product values(null,'雅诗兰黛精华水',200,'c003');
insert into product values(null,'香奈儿香水',350,'c003');
insert into product values(null,'SK-II神仙水',350,'c003');
insert into product values(null,'资生堂粉底液',180,'c003');
insert into product values(null,'老北京方便面',56,'c004');
insert into product values(null,'良品铺子海带丝',17,'c004');
insert into product values(null,'三只松鼠坚果',88,null);
select * from product;
select pname,price from product;
select pname as pn from product;
select distinct price from product;
select pname,price+10 from product;
一般情况下,除非需要使用表中所有的字段数据,最好不要使用通配符‘*’。使用通配符虽然可以节省输入查询语句的时间,但是获取不需要的列数据通常会降低查询和所使用的应用程序的效率。通配符的优势是,当不知道所需要的列的名称时,可以通过它获取它们。 |
---|
MySQL中的SQL语句是不区分大小写的,因此SELECT和select的作用是相同的,但是,许多同学习惯将关键字大写、数据列和表名小写,所以我们也应该养成一个良好的编程习惯,这样写出来的代码更容易阅读和排错。2 |
数据库中的表结构确立后,表中的数据代表的意义就已经确定。
通过MySQL运算符进行运算,就可以获取到表结构以外的另一种数据。
学生表中存在一个birth字段,这个字段表示学生的出生年份。而运用MySQL的算术运算符用当前的年份减学生出生的年份,那么得到的就是这个学生的实际年龄数据。
MySQL支持4种运算符
算术运算符主要用于数学运算,其可以连接运算符前后的两个数值或表达式,对数值或表达式进行加(+)、减(-)、乘(*)、除(/)和取模(%)运算。
select 6 + 2;
select 6 - 2;
select 6 * 2;
select 6 / 2;
select 6 % 2;
select name,price + 10 as new_price from product;
select pname,price * 1.1 as new_price from product;
比较运算符用来对表达式左边的操作数和右边的操作数进行比较,比较的结果为真则返回1,比较的结果为假则返回0,其他情况则返回NULL。
比较运算符经常被用来作为SELECT查询语句的条件来使用,返回符合条件的结果记录。
select * from product where pname = '海尔洗衣机';
select * from product where price = 800;
select * from product where price != 800;
select * from product where price <> 800;
select * from product where not(price = 800);
select * from product where price > 60;
select * from product where price >= 200 and price <=1000;
select * from product where price between 200 and 1000;
“%”:匹配0个或多个字符。
“_”:只能匹配一个字符。
REGEXP运算符用来匹配字符串,语法格式为: expr REGEXP 匹配条件 。
如果expr满足匹配条件,返回1;
如果不满足,则返回0。若expr或匹配条件任意一个为NULL,则结果为NULL。
^ | 匹配以该字符后面的字符开头的字符串 |
---|---|
$ | 匹配以该字符前面的字符结尾的字符串 |
. | 匹配任何一个单字符 |
[...] | 匹配在方括号内的任何字符。例如,“[abc]”匹配“a”或“b”或“c”。为了命名字符的范围,使用一个‘-’。“[a-z]”匹配任何字母,而“[0-9]”匹配任何数字。 |
* | 匹配零个或多个在它前面的字符。例如,“x*”匹配任何数量的‘x’字符,“[0-9]*”匹配任何数量的数字, 而“*”匹配任何数量的任何字符。 |
查询商品价格是200或800的所有商品
select * from product where price = 200 or price = 800;
select * from product where price in (200,800);
查询含有‘裤'字的所有商品
select * from product where pname like ‘%裤%';
查询以'海'开头的所有商品
select * from product where pname like '海%';
查询第二个字为'蔻'的所有商品
select * from product where pname like '_蔻%';
查询category_id为null的商品
select * from product where category_id is null;
查询category_id不为null分类的商品
select * from product where category_id is not null;
使用least求最小值
select least(10, 20, 30); -- 10
select least(10, null , 30); -- null
使用greatest求最大值
select greatest(10, 20, 30);
select greatest(10, null, 30); -- null
逻辑运算符主要用来判断表达式的真假,在MySQL中,逻辑运算符的返回结果为1、0或者NULL
位运算符是在二进制数上进行计算的运算符。
位运算符会先将操作数变成二进制数,然后进行位运算, 最后将计算结果从二进制变回十进制数
数字编号越大,优先级越高,优先级高的运算符先进行计算。
如下图可以看到,赋值运算符的优先级最低,使用“()”括起来的表达式的优先级最高。
如果我们需要对读取的数据进行排序,我们就可以使用 MySQL 的order by 子句来设定你想按哪个字段哪种方式来进行排序,再返回搜索结果。
select
字段名1,字段名2,……
from 表名
order by 字段名1 [asc|desc],字段名2[asc|desc]……
1.asc代表升序,desc代表降序,如果不写默认升序
2.order by用于子句中可以支持单个字段,多个字段,表达式,函数,别名
3.order by子句,放在查询语句的最后面。LIMIT子句除外
select * from product order by price desc;
select * from product order by price desc,category_id asc;
select distinct price from product order by price desc;
聚合函数 | 作用 |
---|---|
count() | 统计指定列不为NULL的记录行数; |
sum() | 计算指定列的数值和,如果指定列类型不是数值类型,那么计算结果为0 |
max() | 计算指定列的最大值,如果指定列是字 select sum(price) from product where category_id = 'c001'; 符串类型,那么使用字符串排序运算; |
min() | 计算指定列的最小值,如果指定列是字符串类型,那么使用字符串排序运算; |
avg() | 计算指定列的平均值,如果指定列类型不是数值类型,那么计算结果为0 |
计算指定列的最大值,如果指定列是字 select sum(price) from product where category_id = 'c001';
符串类型,那么使用字符串排序运算;
之前我们做的查询都是横向查询,它们都是根据条件一行一行的进行判断,而使用聚合函数查询是纵向查询,它是对一列的值进行计算,然后返回一个单一的值;另外聚合函数会忽略空值。
select count(*) from product;
select count(*) from product where price > 200;
select sum(price) from product where category_id = 'c001';
select max(price) from product;
select min(price) from product;
select avg(price) from product where category_id = 'c002';
如果count函数的参数为星号(*),则统计所有记录的个数。
而如果参数为某字段,不统计含null值的记录个数。
这两个函数忽略null值的存在,就如该条记录不存在一样。
max和min两个函数同样忽略null值的存在。
create table test_null(
c1 varchar(20),
c2 int
);
insert into test_null values('aaa',3);
insert into test_null values('bbb',3);
insert into test_null values('ccc',null);
insert into test_null values('ddd',6);
select count(*), count(1), count(c2) from test_null;
select sum(c2),max(c2),min(c2),avg(c2) from test_null;
分组之后对统计结果进行筛选的话必须使用having,不能使用where |
---|
where子句用来筛选 FROM 子句中指定的操作所产生的行 |
group by 子句用来分组 WHERE 子句的输出。 |
having 子句用来从分组的结果中筛选行 |
分组查询是指使用group by字句对查询信息进行分组。
select 字段1,字段2… from 表名 group by 分组字段 having 分组条件;
select category_id ,count(*) from product group by category_id ;
如果要进行分组的话,则SELECT子句之后,只能出现分组的字段和统计函数,其他的字段不能出 现
select 字段1,字段2… from 表名 group by 分组字段 having 分组条件;
select category_id ,count(*) from product group by category_id having count(*) > 1;
分页查询在实比较际中常见,由于数据量很大,显示屏长度有限,因此对数据需要采取分页显示方式。
例如数据共有30条,每页显示5条,第一页显示1-5条,第二页显示6-10条。
select 字段1,字段2... from 表明 limit n
select 字段1,字段2... from 表明 limit m,n
m: 整数,表示从第几条索引开始,计算方式 (当前页-1)*每页显示条数
n: 整数,表示查询多少条数据
select * from product limit 5
select * from product limit 3,5
INSERT INTO SELECT语句:将一张表的数据导入到另一张表中,可以使用INSERT INTO SELECT语句 。
insert into Table2(field1,field2,…) select value1,value2,… from Table1
#或者:
insert into Table2 select * from Table1
#目标表Table2必须存在
SELECT INTO FROM语句: 将一张表的数据导入到另一张表中,有两种选择 SELECT INTO 和 INSERT INTO SELECT 。
SELECT vale1, value2 into Table2 from Table1
//要求目标表Table2不存在,因为在插入时会自动创建表Table2,并将Table1中指定字段数据复制到Table2
中。
SELECT 'abc' REGEXP '^a';
SELECT 'abc' REGEXP 'a$';
SELECT 'abc' REGEXP 'c$’;
SELECT 'abc' REGEXP '.b';
SELECT 'abc' REGEXP '.c';
SELECT 'abc' REGEXP 'a.';
SELECT 'abc' REGEXP '[xyz]';
SELECT 'abc' REGEXP '[xaz]';
SELECT 'a' REGEXP '[^abc]';
SELECT 'x' REGEXP '[^abc]';
SELECT 'abc' REGEXP '[^a]';
SELECT 'stab' REGEXP '.ta*b';
SELECT 'stb' REGEXP '.ta*b';
SELECT '' REGEXP 'a*';
SELECT 'stab' REGEXP '.ta+b';
SELECT 'stb' REGEXP '.ta+b';
SELECT 'stb' REGEXP '.ta?b';
SELECT 'stab' REGEXP '.ta?b';
SELECT 'staab' REGEXP '.ta?b';
SELECT 'a' REGEXP 'a|b';
SELECT 'b' REGEXP 'a|b';
SELECT 'b' REGEXP '^(a|b)';
SELECT 'a' REGEXP '^(a|b)';
SELECT 'c' REGEXP '^(a|b)';
SELECT 'auuuuc' REGEXP 'au{4}c';
SELECT 'auuuuc' REGEXP 'au{3}c';
SELECT 'auuuuc' REGEXP 'au{3,5}c';
SELECT 'auuuuc' REGEXP 'au{4,5}c';
SELECT 'auuuuc' REGEXP 'au{5,10}c';
(abc) abc作为一个序列匹配,不用括号括起来都是用单个字符去匹配.
如果要把多个字符作为一个整体匹配就需要用到括号,所以括号适合上面的所有情况。
SELECT 'xababy' REGEXP 'x(abab)y';
SELECT 'xababy' REGEXP 'x(ab)*y';
SELECT 'xababy' REGEXP 'x(ab){1,2}y';