MySQL方法草稿

create table student(
    id char(36) primary key,
    name varchar(8) not null,
    age int(3) default 0,
    mobile char(11),
    address varchar(150)
)
insert into student 
values ('9b4435ec-372c-456a-b287-e3c5aa23dff4','张三',24,'12345678901','北京海淀');
insert into student 
values ('a273ea66-0a42-48d2-a17b-388a2feea244','李%四',10,'98765432130',null);
insert into student 
values ('eb0a220a-60ae-47b6-9e6d-a901da9fe355','张李三',11,'18338945560','安徽六安');
insert into student 
values ('6ab71673-9502-44ba-8db0-7f625f17a67d','王_五',28,'98765432130','北京朝阳区');
insert into student 
values ('0055d61c-eb51-4696-b2da-506e81c3f566','王_五%%',11,'13856901237','吉林省长春市宽平区');
select * from student


select * from student where age = 11;
select * from student where age >= 10;
select * from student where age is not null;
select * from student where address is null;
select * from student where age = 11 or 28;
select * from student where age =11 or age =28;
select * from student where age between 11 and 28;

#模糊查询
#%可多次匹配
#_只可匹配一次
select * from student where name like '张%' ;
select * from student where name like '张_'

#escape
select * from student where name like '%@%%' escape '@'
select * from student where name like '%@_%' escape '@'
select * from student where age in(11,28)

#排序
#多表排序时,如果前面元素都相同,在排序后面元素
select * from student order by age
select * from student order by age asc
select * from student order by age desc
select * from student order by age, mobile desc
select * from student order by age desc,mobile desc

#伪表dual,通常用来测试一些函数
select now() from dual
select mod(7,6) from dual
drop table student

#distinct 字段只能在distinct后面
select  distinct age from student
select distinct name ,age from student
select name, distinct age from student #error,字段只能在distinct后面

#函数,单行函数
#length 
select length(name) from student
#char_length
select char_length(name) from student
#concat
select concat(id,name,age,mobile,address) from student
#concat_ws
select concat_ws(',',id,name,age,mobile,address) from student
#trim
select trim('   Tom   ');
#substr
select substr('AAATomBBB',3,4);
#replace
select replace('A#AAB#BB#CC#C#','#','$');
#reverse
select reverse('12345ABCDE');


#时间函数
#now
select now();
#date_format
select date_format(now(),'%Y年%m月%d日%H时%i分%s秒');
#strcmp
select strcmp('abc','cba');
#convert
select convert(now(),date);
#if
select if(address is null,'未知',address) from student;
#ifnull
select ifnull(address,'未知') from student;

#多行函数
#count 会忽略空列(null列)
select count(address) from student;#实际上adderss列有5行,可以通过ifnull()函数来解决
select count(ifnull(address,'')) from student;
#group by
select * from student

#group by 查询语句中select 聚合函数,分组字段
select name, count(id) from student group by name


#查询哪个姓名重名
select name, count(id) from student group by name having count(id)>1;

#where 不能跟聚合函数
#where ,group by ,having ,order by

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值