写在前面,本文学习的语句都只是针对数据的增删改查,不涉及对数据库及表本身结构等参数的操作哦;
一、增
1.1 insert into
语法:insert into 库名.表名(列名) values("列值","列值","……");
insert into mydatabase.mytable (name,phone) values ("张三","18659212120");
1.2 insert into + select
语法:insert into 已有的新表名(列名) select 列名 from 原表名;
insert into mynewtable (name) select name from mytable;
1.3 select + into
语法:select 新列名 into 新表名 from 原表名;
select phone into myintotable from mytable;
注:MYSQL 不支持该语句;可用以下语句代替:
create table myintotable(select phone from mytable);
1.4 insert into + select + union + select
语法:insert into 表名(列名) select "列值" union select "列值";
insert into myintotable (phone) select "12345678900" union select "1345678901";
注:union 表示下一行;
二、删
2.1 delete
语法:delete from 表名 where 列名="列值";
delete from myintotable where phone="1345678901";
2.2 truncate
语法:truncate table 表名;
truncate table myintotable;
注:清除表的所有行,但表的结构、列、约束、索引等不会被删除;不能用于有外键约束引用的表;
三、改
3.1 update
语法:update 表名 set 列名="新列值" where 更新条件;
update mytable set phone="18759212120" where name="张三";
四、查
4.1 select + from
语法:select 列名 from 表名;
select * from mytable;
select name from mytable;
4.2 select + from + where
语法:select 列名 from 表名 where 查询条件;
select name from mytable where phone="18659212120";
select * from mydatabase.mytable where phone is NULL;
注:把电话号码这列为空的全部查出来;
4.3 select + from + limit
语法:select 列名 from 表名 limit 行数;
select * from mytable limit 2;
注:只查示前两行数据;
4.4 select + from + order by
语法:select 列名 from 表名 order by 列名
select * from scoretable order by score;
注:针对 score 列进行数据排序,默认为asc升序,末尾加 "desc" 参数更改为降序;
4.5 select + from + group by
语法:select 列名 from 表名 group by 列名
select * from scoretable group by name;
注:针对 name 列进行进行分组查询;
4.6 select + from + where + like
语法:select 列名 from 表名 where 列名 like 列值
select * from mytable where name like "李%";
注:把name列 "李" 字打头的全部查出来;
4.7 select + from + where + between
语法:select 列名 from 表名 where 列名 between 列值
select * from scoretable where score between 75 and 89;
注:把分数在75至89之间的数据全部查出来;
4.8 select + from + where + in
语法:select 列名 from 表名 where 列名 in 列值
select * from scoretable where score in ("59","60","100");
注:把分数分别为“59”或者“60”或者“100”的数据全部查出来;
4.9 多表内连接查询
语法:select 表名1.表名1列名,表名2.表名2列名 from 表名1,表名2 where 查询条件;
select mytable.name,mynewtable.name from mytable,mynewtable where mytable.name = mynewtable.name;
注:把表1与表2名字一样的数据全部查出来;
4.10 多表外连接查询
……
结束词:SQL语句还有N多种语法,特别是查询语句,以上为常用到的一些语句,有兴趣的可以参阅以下资料:http://dev.mysql.com/doc/refman/5.1/zh/index.html