1、服务器mysql进入
1、启动mysql服务器
net start mysql
2、关闭
net stop mysql
3、进入
mysql -h 主机地址 -u 用户名 -p 用户密码
4、退出
exit
2、查询语句
1、查询a表
select a,b,c from a;
2、in
select a,b,c from a where a IN (1,2,3);
3、BETWEEN操作
SELECT f_name, f_price FROM fruits WHERE f_price BETWEEN 2 AND 10;
4、左连接LEFT JOIN
select a.a, a.b, a.c, b.c, b.d, b.f from a LEFT JOIN b ON a.a = b.c
5、右连接right JOIN
select a.a, a.b, a.c, b.c, b.d, b.f from a right JOIN b ON a.a = b.c
6、查询以b开头的记录
SELECT f_name FROM fruits WHERE f_name LIKE 'b%';
7、查询f_name字段为NULL的记录
SELECT f_name FROM fruits WHERE f_name IS NULL;
8、ORDER BY DESC/ASC: 降序/升序排序
SELECT f_name FROM fruits ORDER BY f_name DESC;
9、根据s_id对记录进行分组:
SELECT s_id, COUNT(*) AS Total FROM fruits GROUP BY s_id;
10、显示每个分组中的字段值
SELECT s_id, GROUP_COUNT(f_name) AS names FROM fruits GROUP BY s_id;
11、显示f_name数量大于1的分组信息。
SELECT s_id, s_name FROM friuts GROUP BY s_id HAVING COUNT(f_name) > 1;
12、显示分组后数据的所有记录
SELECT s_id, COUNT(*) FROM fruits GROUP BY s_id WITH ROLLUP;
13、内连接查询(INNER JOIN):
SELECT t_1.name, s_name FROM fruits, t_1 WHERE fruits.s_id = t_1.s_id;
SELECT t_1.name, s_name FROM fruits INNER JOIN t_1
ON fruits.s_id = t_1.s_id;
14、自连接查询。
SELECT f1.f_name, f1.s_id FROM fruits AS f1, fruits AS f2 WHERE
f1.s_id = f2.s_id;
15、大于子查询结果中最小值的所有记录
MYSQL> SELECT * FROM fruits WHERE s_id > ANY(SELECT s_id FROM t2);
16、大于子查询结果中最大值的所有记录
SELECT * FROM fruits WHERE s_id > ALL(SELECT s_id FROM t2);
17、子查询是否有记录
SELECT * FROM fruits WHERE EXISTS(SELECT s_id FROM t2);
18、如果子查询没有返回行则符合条件
SELECT * FROM fruits WHERE NOT EXISTS(SELECT s_id FROM t2);
19、IN/NOT IN:
MYSQL> SELECT * FROM fruits WHERE s_id IN(SELECT s_id FROM t2);
20、<、<=、=、>=、!=:
SELECT * FROM fruits WHERE s_id = (SELECT s_id FROM t2 WHERE s_id = 1);
21、UNION 不删除重复行,
SELECT s_id FROM fruits WHERE f_price < 9 UNION SELECT s_id
WHERE s_id IN(10,11);
22、UNION ALL删除重复行
SELECT s_id FROM fruits WHERE f_price < 9 UNION ALL SELECT s_id
WHERE s_id IN(10,11);
3、插入语句
1、指定字段
insert into table_name(column1,column2)values(value1,value2);
2、所有字段
insert into table_name values(value1,value2);
3、批量插入数据
insert into table_name values (value1,value2),(value1,value2);
4、指定插入批量数据
insert into table_name(column1,column2) values(value1,value2),(vae1,vae2)
5、满足了指定条件时才插入数据
insert into dept(deptno,dname,loc) select 11,dept.dname,dept.loc from
dept where deptno=22;
4、更新语句
1、更新一个字段,将users表中id等于123的记录的age改为24
UPDATE users SET age = 24 WHERE id = 123;
2、更新多个字段的值
UPDATE users SET age = 24, name = 'Mike' WHERE id = 123;
3、修改表名(将数据表 testalter_tbl 重命名为 alter_tbl)
ALTER TABLE testalter_tbl RENAME TO alter_tbl;
4、表tb_test01增加 增加字段 id_01 类型限制 int(10)
ALTER TABLE tb_test01 add id_01 int(10);
5、 增加字段 id_02 并且放在第一个字段
ALTER TABLE tb_test01 add id_02 int(10) FIRST
6、 增加字段 id_03 并且放在id 后面
ALTER TABLE tb_test01 add id_03 int(10) AFTER id
5、删除语句
1、删除表test,并释放空间,将test删除的一干二净。
DROP TABLE test;
2、删除表test里的内容,并释放空间,但不删除表的定义,表的结构还在
TRUNCATE test;
3、删除表test内的所有内容,保留表的定义,不释放空间。
DELETE FROM table1;
DELETE FROM table1 WHERE id=1;
6、视图创建,表复制
1、视图创建
create view v as (select * from table1) union all (select * from table2);
2、表复制
CREATE TABLE xinbiao SELECT * FROM jiubiao
7、索引创建
1.添加PRIMARY KEY(主键索引)
ALTER TABLE `table_name` ADD PRIMARY KEY ( `column` )
2.添加UNIQUE(唯一索引)
ALTER TABLE `table_name` ADD UNIQUE ( `column` )
3.添加INDEX(普通索引)
ALTER TABLE `table_name` ADD INDEX index_name ( `column` )
4、添加FULLTEXT(全文索引)
mysql>ALTER TABLE `table_name` ADD FULLTEXT ( `column`)
5.添加多列索引
mysql>ALTER TABLE `table_name` ADD INDEX index_name ( `column1`,
`column2`, `column3` )
8、事务使用
9、账户权限添加修改
1、赋予所有特殊权限给root用户,可以从任何IP地址远程登录,密码为abc123,
且拥有grant赋予权限的权限
grant all privileges on *.* to 'root'@'%' identified by 'abc123' with
grant option;
flush privileges;
10、Cast函数用法
Cast(字段名 as 转换的类型 )
例如表table1
date
2015-11-03 15:31:26
select cast(date as signed) as date from table1;
date
20151103153126
select cast(date as char) as date from table1;
date
2015-11-03 15:31:26
select cast(date as datetime) as date from table1;
date
2015-11-03 15:31:26
select cast(date as date) as date from table1;
date
2015-11-03
select cast(date as time) as date from table1;
date
15:31:26
select unix_timestamp(now()) 现在的时间戳
1586412498
11、case when
1、条件判断
SELECT NAME '英雄',
CASE NAME WHEN '德莱文' THEN '斧子'
WHEN '德玛西亚-盖伦' THEN '大宝剑'
WHEN '暗夜猎手-VN' THEN '件'
ELSE '无'
END '装备' FROM user_info;
2、行转列
SELECT id,idd,
sum( CASE name WHEN '大学语文' THEN scores
ELSE 0 END ) '大学语文',
sum( CASE co.name WHEN '高等数学' THEN scores
ELSE 0 END ) '高等数学'
FROM edu_student
GROUP BY id
ORDER BY NULL;
12、 date_sub()函数:
select now(); 表示获取当前时间
今天是2013年5月20日。
date_sub('2012-05-25',interval 1 day) 表示 2012-05-24
date_sub('2012-05-25',interval 0 day) 表示 2012-05-25
date_sub('2012-05-25',interval -1 day) 表示 2012-05-26
date_sub('2012-05-31',interval -1 day) 表示 2012-06-01
date_sub(curdate(),interval 1 day) 表示 2013-05-19
date_sub(curdate(),interval -1 day) 表示 2013-05-21
date_sub(curdate(),interval 1 month) 表示 2013-04-20
date_sub(curdate(),interval -1 month) 表示 2013-06-20
date_sub(curdate(),interval 1 year) 表示 2012-05-20
date_sub(curdate(),interval -1 year) 表示 2014-05-20
13、 date_add()函数
set @dt = now();
select date_add(@dt, interval 1 day); -- add 1 day
select date_add(@dt, interval 1 hour); -- add 1 hour
select date_add(@dt, interval 1 minute); -- ...
select date_add(@dt, interval 1 second);
select date_add(@dt, interval 1 microsecond);
select date_add(@dt, interval 1 week);
select date_add(@dt, interval 1 month);
select date_add(@dt, interval 1 quarter);
select date_add(@dt, interval 1 year);
select date_add(@dt, interval -1 day); -- sub 1 day
14、dayofweek(), dayofmonth(), dayofyear():在一周、一月、一年中的位置。
set @dt = '2008-08-08';
select dayofweek(@dt); -- 6
select dayofmonth(@dt); -- 8
select dayofyear(@dt); -- 221
#日期 '2008-08-08' 是一周中的第 6 天(1 = Sunday,
2 = Monday, ..., 7 = Saturday);一月中的第 8 天;一年中的第 221 天
15、dayname(), monthname()函数
set @dt = '2008-08-08';
select dayname(@dt); -- Friday
select monthname(@dt); -- August
16、 last_day() 函数:
select last_day('2008-02-01'); -- 2008-02-29
select last_day('2008-08-08'); -- 2008-08-31
MySQL last_day() 函数表示当前月份中有多少天,
17、日期时间转换函数
1. MySQL (时间、秒)转换函数:time_to_sec(time), sec_to_time(seconds)
select time_to_sec('01:00:05'); -- 3605
select sec_to_time(3605); -- '01:00:05'
2. MySQL (日期、天数)转换函数:to_days(date), from_days(days)
select to_days('0000-00-00'); -- 0
select to_days('2008-08-08'); -- 733627
select from_days(0); -- '0000-00-00'
select from_days(733627); -- '2008-08-08'
3. MySQL Str to Date (字符串转换为日期)函数:str_to_date(str, format)
select str_to_date('08/09/2008', '%m/%d/%Y'); -- 2008-08-09
select str_to_date('08/09/08' , '%m/%d/%y'); -- 2008-08-09
select str_to_date('08.09.2008', '%m.%d.%Y'); -- 2008-08-09
select str_to_date('08:09:30', '%h:%i:%s'); -- 08:09:30
select str_to_date('08.09.2008 08:09:30', '%m.%d.%Y %h:%i:%s');
-- 2008-08-09 08:09:30
测试工作软件包下载,要不知道下载哪个就都下载下
软件包:https://pan.baidu.com/s/1NxB2HaGRrRs9ERVu9w3SvQ
提取码:nn5j