-- insert into table_name select ... -- 要求表 socrebak 必须存在insertinto socrebak select*from socre where neza='neza';-- create table table_name select ...-- 要求表 socrebak 不存在createtable socrebak select*from socre where neza='neza';-- select * into socrebak from socre where neza='neza'; # mysql 不支持
五、通配符
通配符
描述
说明
%
任意个字符
配合 like
_
一个字符
配合 like
[]
字符集中任何一个单一字符
配合 regexp 或者 rlike,正则表达式
select*from mm where name like'abc%';select*from mm where name like'abc_';select*from mm where name regexp'^[^abcde]';select*from mm where name rlike'^[^a-e]';
六、别名
-- as 可以省略-- 把多个字段放在一起的时候用 concat()select name 网站名, concat(url,', ', alexa,', ', country) 网站信息 from websites;selectdate 日期 from access_log;-- 表的别名select w.name, w.url, a.count, a.datefrom websites w, access_log a where a.site_id=w.id and w.name='123';
七、join
select w.id 网站id, w.name 网站名, a.count 网站用户, a.date 日期 from websites w join access_log a on w.id=a.site_id;-- 在使用 join 时,on 和 where 条件的区别:-- 1、on 条件是在生成临时表时使用的添加,它不管on中的添加是否为真,都会返回左表中的记录;-- 2、where 条件是在临时表生成好之后,再对临时表进行过滤的条件,这时已经没了 left jion 的含义(必须返回左表的记录)了,条件不为真的就全部过滤掉;
-- select ..... from .... where .... group by .... having ... order by .... limit ....;select
w.name,sum(a.count) 数量
from
websites w join access_log a on w.id=a.site_id
where
w.alexa<200groupby
w.name
havingsum(a.count)>200orderby
name desclimit1,1;
dropfunctionifexists get_alexa;delimiter $$ -- 定义结束符,函数内部回用到;以防冲突createfunction get_alexa(wid int)returnsintbegindeclare result int;select alexa into result from websites where id=wid;return result;end$$
delimiter;-- 调用select get_alexa(2);
十一、case … when … then … else … end
-- case [字段名]-- when 条件1 then 选项1-- when 条件2 then 选项2-- else 默认值-- end-- end 后面最好加上一个别名select
name, url,casewhen alexa between10and30then'alexa在10到30之间'when alexa <10then'alexa小于10'when alexa >30then'alexa大于30'else'其他'end 判断alexa的范围
from
websites;select
name, url,case
alexa when20then'alexa在10到30之间'when3then'alexa小于10'when0then'0'else'其他'endfrom
websites;