MySql教程

五,MySQL

本文主要是介绍五,MySQL,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

MySQL:

导入数据:cmd---sqlplus---用户和口令---将数据粘贴到sql>后面【库名=d*,表名=t*, 列名=a*, 值=v*】select 名称_version(); 查看版本create database if not exists d1;show databaases;use d1;select databse ();
create table t1(id int primary key,name varchar(30),age int,gender varrchar(10),birthday date);
索引 create index 索引名称 on 表名(列名);create index t2_num on t2(name);show index from t2;drop index t2_num on t2;
序列 sequencecreate sequence tid_seql;

create table t2 like t1; 复制表结构insert into t2 select * from t1; 复制表内容insert into t2(name) select * from t1; 复制t1中name的数据
alter table f1 ADD a1 数据类型
ALTER TABLE 表名 MODIFY 列名 新的类型;ALTER TABLE 表名 CHANGE 旧列名 新列名 类型;ALTER TABLE 表名drop 列名;DENAME TABLE 表名 to 新表名;
show tables;show tables from d1;show create table t1;show full columns from t1;desc f1;
insert into t1 values    (1,'tom',23,'男','20190807'),    (2,'bob',27,'男','20121107'),    (3,'jay',25,'男','20171207'),    (4,'cuky',23,'女','20190808'),    (5,'davi',22,'男','20200907');
insert into f1(id,name,birthday) values    (6,'simth',30,'男','20171207');    (7,'na',27,'女','20200907');    (8,'me',22,'男','20200907');    (9,'viya',23,'女','20171207');    (10,'rose',22,'女','20190807');
select * from f1;
update f1 set name='pp',age=40;(如果没有条件,则默认全表)update f1 set a1=v1,a2=v2 where a3=v3;update f1 set name='jak',age=23 where id=3;update f1 set sex='l' where id in(7,8,9,10);

delete from f1 where 条件 truncate table f1; 无法恢复drop table f1;drop database d1;
无条件查询:select * from f1;select id,name from f1;
别名?mysql 空值的处理方法?
select * from f1 order by a1 asc/desc;select id,name from f1 order by id asc;select id,name,birthday from f1 order by id asc,birthday desc;
select distinct a1 from f1;
带条件查询:select * from f1 where 条件; (>,<,=,>=,<=,<>,!=)select * from f1 where id>5 order by birthday desc;select id,name,age from f1 where id=1;
select * from f1 where a1 is null; (列值为空的)select * from f1 where a1 is not null; (列值不为空的)select * from f1 where name like '%n%'; ( 'n%' '%n' 'n__' '_n_' '__n') 包含select * from f1 where score between 70 and 80; (包含初值和终值)select * from t2 where id in(3,8); (多个列值)select * from t2 where name in('a','d','g','h');
select id,name,age,score from f1 where score>10 and score<=80;    ( and or not)select id,name,age,score from f1 where sex like 'l' and score is not null;select id,name,sex,age,score from f1 where sex like 'm' or score is null order by age asc;
select id,name,sex,age,score,age*12+score from f1;    聚合函数 count(),sum(),avg(),min(),max() (不为null的)select count(*) from f1;select count(score) from f1;select count(distinct sex) from t2; (distinct 类型的个数)
select count(*),sum(score),avg(score),min(age),max(score) from t2;select count(*) c,sum(score) s,avg(score) a,min(age) m,max(score) m1 from t2; (别名)
分组select id,name,sex,sum(age),avg(score),count(*) from f1group by idorder by count(*) asc,avg(score) desc;
select id,name,sex,max(age),avg(score) from t2 where sex not like '%m%' group by id order by count(*) asc,avg(score) desc;
where 针对整张表全部数据进行过滤,而having在group by 后面使用,针对分组后的数据再次进行过滤selectfromwheregroup byhavingorder by
select id,name,sex,sum(age),avg(score) from t2 group by id having avg(score)>60 order by count(*) desc,avg(score) asc;
select id,name,sex,sum(age),avg(score) from t2 where name not like 'k%' and age>22 group by id having max(score)<85 order by id asc;
常用字符处理函数:
length(列名/字符个数)select name,length(name) from f1;select length('helloword') from dual; (dual 虚拟表常用)
trim(列名/字符串) 去掉字符串两端的空格select trim( 'nihao' ) from dual;
substr(参数1,参数2,参数3); 用于字符串的截取参数1:处理的字符串/列名参数2:如果是整数,表示从正数第几个开始截取(从1开始),如果是负数,表示从倒数第几个开始截取参数3:表示截取的字符个数select substr('helloword',3,2) from dual; llselect substr('helloword',-2,2) from dual; rdselect name,substr(name,-2,2) from t1;
串联字符串 ||select '您'||'好' from dual;
concat(字符串1,字符串2)select concat('abcd','efghi') from dual;select id,name,concat(id,name) from t1;
常用数值处理函数round(数值,位数)四舍五入函数 (如位数>0,小数点保留几位小数;位数=0,不保留小数,位数<0,小数点教之前的第几位进行四舍五入)select round(45.926,2) from dual; 45.93select round(45.926,0) from dual; 46select round(45.926,-1) from dual; 50select round(45.926,-2) from dual; 0
mod 求出余数select mod(10,3) from dual; 1 (10/3=3 余1)
sqrt 求出平方根select sqrt(100) from dual;
power 求成乘方select power(2,3) from dual; 8 (2的3次方等于8)
连接查询selectfromwhere
等值连接 (where等值条件)根据id等值条件找到t1表中的id ,name,region 和t2表中的name ,regionselect a.id,a.name,a.region,b.name,b.region from t1 a,t2 b where a.id=b.id;
select a.id,a.name,a.region,b.region,b.birthday from t1 a,t2 b where a.id=b.id;select a.*,b.*,c.* from t1 a,t2 b,t3 c where a.id=b.id and c.id=b.id;
select a.id,a.name,a.region,b.region,b.birthday from t1 a,t2 b where a.id=b.id order by a.id desc;
select a.id,a.name,a.region,b.region,b.birthday from t1 a,t2 b where a.id=b.id and b.region='a' order by a.id desc;
非等值连接(where 非等值,包含)
自连接select a.id,a.name,a.region,b.name,b.region from t1 a,t1 b where a.id=b.id2;
子查询(嵌套查询:一个查询语句里再嵌套一个查询语句)select id,name,region,birthday from t2 where id>(select avg(id) from t2); 按高于id的平均值查询t2表中 id,name,region,birthday
select id,name,region,birthday from t2 where id=(select id from t1 where name='tom'); 按照t1表的name='tom'找t2表中的name='tom'的id,name,region,birthday

这篇关于五,MySQL的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!