MySql基础总结(一)

添加语句

  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()获取系统当前的时间

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • 什么是数据库? 数据库是存储数据的集合的单独的应用程序。每个数据库具有一个或多个不同的API,用于创建,访问,管理...
    chen_000阅读 9,483评论 0 19
  • MySQL学习笔记---基础概念和一些基础SQL语句(一) 作者:zhangjunqiang MySQL学习笔记,...
    Omit03阅读 4,332评论 1 3
  • 50个常用的sql语句Student(S#,Sname,Sage,Ssex) 学生表Course(C#,Cname...
    哈哈海阅读 5,037评论 0 7
  • 50个常用的sql语句 Student(S#,Sname,Sage,Ssex) 学生表 Course(C#,Cna...
    最美的太阳WW阅读 8,545评论 0 23
  • 又走过 这两棵银杏树 风一来 金黄的叶慢慢飘落 我悔 在如此浪漫的场地 没有和你发生点什么故事 就匆匆散去 我要拿...
    夏霖捷阅读 1,598评论 0 0

友情链接更多精彩内容