1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 |
1.查询 select 列名 from 表名 where; select * from table1(where id = 5); distinct//去重; select distinct id from table1; 查询并分组 select count(*) from table1 (where id > 5);//统计个数,where 限定条件 select id,count(*) from table1 group by id;//按照id分组 having 分组 select id from info group by id having count(id)>1; 查询后排序//不指定则默认升序 select * from info order by id asc, name desc;//id升序,id相同按照name降序 limit限制 select * from info limit 3;//前三行 select * from info limit 4,5;//第四行开始的五行 select name,count(*) from info group by name desc limit 1; 2.where { id > 5; id = 5; 运算符 = 等于 <>不等于 != 不等于 < > <= >= between and//between 5 and 6; is null;//检查空值,where id is null; and , or// 链接多个运算符,and 的运算级别比or高,课余括号连用 //例如where id > 5 and ( name < 9 or age > 8); in, not in //id in(5,8), id not in (5,8);匹配 } 3.like通配符{ % 匹配0,1,多个任意字符 _ 只能匹配1个字符 } 4.regexp正则表达式{ where name regexp '.三'; | 匹配几个字符串其中之一//例如'.pple|.dog|.cat'; 对于元字符,需要转义系列, 例如\\ \. \[ \] \+ \* \? [] 匹配多个字符的其中一个 例如 [abcd]pple [Aa]pple 匹 配区间 例如 [a-z]pple [0-9]pple [A-Z]pple [0-9a-zA-Z] [^0-9]//出0-9以外的字符 [[:alnum:]] [[:alpha:]] [[:digit:]] [[:lower:]] [[:uppper:]] . 匹配任何字符 + 匹配一个或多个字符 * 匹配0个或多个字符 ? 匹配0或1个字符 {} 匹配重复次数 例如 a{0,4}, 最少出现0次,最多4次 a{3,}, 最少出现4次 a{3} 匹配3次 } |
创建计算字段
1 2 3 4 5 6 7 8 9 10 |
1.拼接字段 { select concat (name , '年龄为:', age ) from info;//拼接姓名与年龄 select concat (name , '年龄为:', age ) as new_colunm from info; //为这个拼接列起个名 } 2.计算字段{ + - * / 加减乘除都可用 select age * age from info;//计算字段 select age * age as new_colunm from info ;//为这个字段列起个名字 } 3.rtrim()//去掉末尾空格。例如rtrim(id); |
分组查询
1 2 3 4 5 6 7 8 9 10 11 12 13 |
select id, count(id), avg(id) from info group id;//按照id分组,并显示每组的数量和平均值 where 按照行过滤,having按照组过滤 having{ select id , count(*) , max(id) from info group by id having count(*) > 2; } where + having{ select id , count(*) , max(id) from info where age > 90 group by id having count(*) > 2;//where把年龄90岁下的过滤了,having不能检测到90岁以下的了,count比不用where少 } 分组的同时可以排序{ select id , count(*) , max(id) from info group by id having count(*) > 2 order by id ; } |
子查询 和 表联结
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
想查询的数据列不在同一张表上{ info中有 id, name info2中有 id, age 查询张三的年龄{ select age from info2 where id = (select id from info where name = '张三'); select age from info2 where id in (select id from info where name = '张三'); 如果子查询的结果可能不唯一,可以用in//not in 也是可以适当用的 } 想查张三的年龄,姓名和id select id, name, (select age from info2 where info.id = info2.id) where id = '张三'; //对于用重复的列名要用表名限定 select id, name, age form info, info2 where info.id = info2.id; select info.id, info.name, info2.age form info, info2 where info.id = info2.id; //从笛卡尔积中过滤 内连接{ select id, name, age form info inner join info2 on info.id = info2.id; //与where作用相同,从笛卡尔积中过滤 } 子查询和联结表从多个表中的笛卡尔积中过滤,与单表过滤语法类似 自联结{ 想知道一个表中,某个学生的辅导员的其他学生的年龄 select age from info where fudaoyuan = (select fudaoyuan where name = '张三'); select p1.age from info as p1, info as p2 where p1.fudaoyuan = p2.fudaoyuan and p2.name = '张三';//吧一张表看成两个表,起个名字,从笛卡尔积中过滤; } } |
组合查询
1 2 3 4 5 6 |
union{union会过滤重复的行,不想过滤用union all 把两个查询语句放一起,查询结果放在一个表中 select * from info union select * from info2; } |
视图
1 2 3 4 5 6 7 8 9 10 11 12 |
创建视图{ creat view 视图名字 as select ................ 例如 create view test as select id, age2 from info, info2; 本次操作结果是把数据添加到一个视图里(这是一张特殊的表) 查看具体情况 desc test 查看创建语句 show creat table test 查看创建语句 show create view test show tables 则会显示所有的表和视图 更改视图名字 rename table test to test1; 总之视图是一张表,特殊的表,视图继承自表; } |