mysq笔记

create database if not exists test; 为了不产生错误,创建数据库时加上一些 简单的逻辑判断

drop database if exists test; 为了不产生错误,在删除数据库时加上一些简单的逻辑判断

show database; 查看数据库列表

create  table  if not exists test(id varchar(32)); 为了不产生错误,在创建表格时加上一些简单的逻辑判断

drop table test; 删除表格

insert into table_name(id,...,...)values(1,...,....),(2,...,...); 向表格内插入数据

 

schemata 所有的库

tables  所有的表

columens  所有的列

select * from user where name ='XXX‘;

select * from user(表名) where(+条件) id = '1' and  name = 'lisi';

select * from user  where  xxx and xxx or xxx;

and  : true ,false ,数字,null,条件语句

   = ,!=,<,>,<>,like+(%)模糊查询

 

select count(*) as(重命名) 'xxx',列名,222 from user where name = 'xxx'; 查询条数

select conat(id,name) from user where id; 拼接

select substr(列名,起始位置,偏移量) from user where id=1;截取字符串

select length(name) from user where id=1; 字符串的位数

select substr(name,length(name)-2,3) from user;

select substr (name,-3,1) from user; 截取倒数第三位

select min(id) from user; 取列内最小值

select avg(id) from user; 取列内平均值

select sum() from user; 取列内所有值之和

select group_concat(name) from user; 把一列全部列出

字符串函数

select ascii();ascii函数

select left(str,x);返回str字符串中最左边的x个值

select position(int in substr);第一个字符串在第二个的哪个位置

控制流函数

select case when 1>0 then 'big' else 'small' end; 如果1>0 则输出 big 否则 输出 small

select if(1>0,1,2);如果1>0输出1,否则输出2

联合查询

join 内关联

left join 左关联

right join 右关联

inner join 内关联

xxx  union  xxx

查询列的个数必须一致 union select  生成一个临时表

select name,grade from user u left join grade g on u.id=g.user_id; 把user中的name和grade中的grade用left join(左关联)关联起来,用的是id

 

select * from user order by name; 按照名字排序  asc 升序  aesa 降序

posted @ 2018-04-16 18:45  Blizzard-wow  阅读(140)  评论(0编辑  收藏  举报