MySQL函数

--1. mid()---从文本字段中提取字符;
/*
column_name    必需。要提取字符的字段。
start    必需。规定开始位置(起始值是 1)。
length    可选。要返回的字符数。如果省略,则 MID() 函数返回剩余文本。
*/
select mid(user,1,2) from mysql.user;
select mid(user,2) from mysql.user;
--2. limit()---返回前几条或者中间某几行数据。第0条开始取3条
select user from mysql.user limit 0,3;
--3. concat连接字符串。只要其中一个是NULL,那么将返回NULL
 select concat('123','456');
 select concat('123',null);
--4. concat_ws函数在执行的时候,不会因为NULL值而返回NULL
select concat_ws('123',null);
select concat_ws('123','abc');
select concat_ws('123','abc','.');
--5. group_concat([DISTINCT] 要连接的字段 [Order BY ASC/DESC 排序字段] [Separator '分隔符'])
create table mysql.aa(id int,name int);
insert into mysql.aa(id,name) values(1,10),(1,20),(1,20),(2,20),(3,200),(3,500);
select id,group_concat(name) from mysql.aa group by id;
--6. rand()---用于产生一个0~1的随机数
select rand(),rand();
select host,user from mysql.user group by rand();
--7. floor()---向下取整
 select floor(123.456),floor(-123.456);
--8. group by---依据我们想要的规则对结果进行分组
select host,user from mysql.user group by user;
select host,user from mysql.user group by host;
--9. length()---返回字符串的长度
select host,user from mysql.user where length(user)<6;
--10. Substr()---截取字符串 三个参数 (所要截取字符串,截取的位置,截取的长度)
select substr('123456',2,3);
select substr(user,1,3) from mysql.user;
-- 11. Ascii()---返回字符串的ascii码
select ascii(2);
select ascii(substr(user,1,1)) from mysql.user;
--1. 获取数据库   floor报错
select count(*),concat(0x3a,table_schema,0x3a,floor(rand()*2)) from information_schema.tables group by table_schema;
select count(*),concat(0x3a,(select column_name from information_schema.columns where table_name='aa' limit 0,1),0x3a,floor(rand()*2)) from information_schema.tables group by table_schema;
select count(*),concat(0x3a,0x3a,(select table_name from information_schema.tables where table_schema='mysql' limit 3,1),0x3a,floor(rand()*2)) name from information_schema.tables group by name;
select count(*),concat(0x3a,0x3a,(select username from dbsong.users limit 0,1),0x3a,floor(rand()*2)) name from information_schema.tables group by name;
--查看表名
 select table_name from information_schema.tables where table_schema=database() limit 0,1;
--查看表名的第一个字符
select substr((select table_name from information_schema.tables where table_schema=database() limit 0,1),1,1) m;
--查看表名第一个字符的ASCII
select ascii(substr((select table_name from information_schema.tables where table_schema=database() limit 0,1),1,1)) m;


 

你可能感兴趣的:(MySQL)