创建表
CREATE TABLE `users` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`code` char(6) NOT NULL DEFAULT "",
`name` varchar(32) NOT NULL DEFAULT "",
`city` VARCHAR(32) NOT NULL DEFAULT "",
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
添加语句
1.insert[into] tablename(field1,field2,field3......) values(data1,data2,data2.....), (data1,data2,data2....),(.......);
2.replace[into] tablename(field1,field2,field3......) values(data1,data2,data2.....), (data1,data2,data2....),(.......);(说明:当插入的数据的主键或者唯一键有重复的时候,变成修改该行数据)
删除语句
1.delete from tablename (where条件)(order by排序字段)(limit限定)
修改语句
1.update tablename set field1=value1,field2=value2,field3=value3,......(where条件)(order by排序字段)(limit限制行数)
查询语句
1.select (all | distinct) 字段或者表达式 (from子句) (where子句) (group by子句) (having子句) (order by子句) (limit子句);
结构解释:
1.from子句:构成提供给select的数据源,所以一般后面写表名,例如:
select * from tablename1;
select * from tablename1,tablename2;
2.where子句:where子句是对数据源中的数据进行过滤的条件设置,那么表示where子句是依赖于获取数据源的from子句的,where子句中可以使用各种"运算符",例如:
select * from tablename where uid>100 and age<50;
select * from tablename where not(city='1');
select * from tablename where cid>100 or sex=0;
//需要注意的运算符 is in like between
//is
select * from tablename where telephone is not null;
//between 判断数据在某个连续的范围内(比较适合数字类型的数据)
select * from tablename where age between 20 and 60
//in 字段的值在是所列出的值中的一个,可以用于一些无序的散列值
select * from tablename where rank in (1,3,5,7);
//like 对字符串进行模糊查找
//语法格式: field like "%keyword%"
//content like "%gong%" 查找包含gong的所有数据
//content like "%gong" 查找以gong结尾的所有数据
//content like "gong%" 查找以gong开头的所有数据
//content like "gong_" 查找以gong开头的并且只有两个字符长度的所有数据
//content like "_gong" 查找以gong结尾的并且只有两个字符长度的所有数据
select * from tablename where username like "%gong%";
3.group by子句:对通过where子句过滤后的数据根某种标准(依据)进行分组
//语法结构 group by field1[desc|asc],field2[desc|asc]
select * from user where uid>100 and sex=0 group by viprank;
4.having子句:对分组后的的数据进行过滤,作用和where类似
select count(*) as 总量,max(price) as 最高价,min(price) as 最低价,avg(price) as 平均价 from goods group by type having 总量>10;
5.order by子句:对取得的数据结果以某个标准(字段)进行排序,默认是asc(正序)
//语法结构 order by field1 [asc|desc],field2[asc|desc]...
select * from user where telephone is not null order by uid desc;
6.limit子句:对取出来的数据进行数量的限制,从第几行开始取出来几行数据
select * from goods where num>0 limit 0,20;
7.comment:
①. all | distinct 用于设置select出来的数据是否消除重复行,默认是all既不消除所有的数据都出来;distinct表示会消除重复行
②. concat()是mysql的系统函数,用于连接多个字符串
③. now()获取系统当前的时间
作者:captain89
链接:https://www.jianshu.com/p/02d7f2e1ab00
感谢作者总结,仅方便自己查看转载