Mysql增删改查

Mysql比较重要的便是这四个字了,增、删、改、查,所以单独列一个来写好了。接下来时第一个操作——增。
<b>增——插入记录</b>
基本语法一:isnert into 表 values(值1,值2,...,值n);
<code lang="mysql">insert into user values(0,'张三','男')</code>
说明:向user表中加入一个id为0,名字为张三,性别为男的的一行数据。
基本语法二:insert into 表(字段1,字段2,...字段n) values(值1,值2,...,值n);
这两个基本语法的区别就是语法一表中有多少个字段就必须要插入多少个值,如果有默认值,不想传,可以写null;但是基本语法二按照输入的字段来输入值,这些值必须填写,其他的字段会自动补充默认值。
比如基本语法1:
<code lang="mysql">
insert into user values(0,‘张三','男',null)
</code>
基本语法2:
<code lang="mysql">
insert into user(id,name,sex)  values(0,'张三','男')
</code>
<b>删——删除记录</b>
基本语法一:delete from 表 [where 条件];
<code lang="mysql">delete from user where id>10</code>
说明:在user表中删除id>10的所有记录
注意:如果没有加上where条件,delete将会清空整个表的记录!!!!!
基本语法二:TRUNCATE TABLE 表;
<code lang="mysql">TRUNCATE TABLE user</code>
说明:清空表的数据,并且让自增的id从1开始自增
delete可以返回被删除的记录数,而TRUNCATE TABLE返回的是0,如果表中有自增字段,使用truncate table之后自增字段的初始值恢复为1。
<b>改——更新记录</b>
基本语法一:update 表名 set 字段1=值1,字段2=值2,...,字段n=值n where 条件
<code lang="mysql">update user set name='Carry',sex='female' where id=10</code>
说明:在id为10的地方将name字段改成Carry,sex字段改成female
基本语法二:update 表1,表2 set 字段1=值1,字段2=值2,...字段n=值n where 条件
<code lang="mysql">update money m,user u set m.balance=m.balance*u.age where m.id=u.id</code>
说明:将money表的别名设置为m,user表的设置为u,在m.id=u.id时,将m.balance =m.balance*u.age。
<b>查——查询记录</b>
基本语法一(基础查询):select * from 表;
<code lang="mysql">select * from money</code>
说明:查询表中所有字段的所有结果
基本语法二(指定字段查询):select 字段 from 表;
<code lang="mysql">select id,name from money</code>
说明:查询money中id,name字段中的所有结果
基本语法三(单个不重复记录字段查询):select distinct 字段 from 表;
<code lang="mysql">select distinct age from money</code>
说明:查询表中年龄唯一的所有结果,其实就相当于在指定字段查询的功能上添加了去重功能
基本语法四(条件查询):select 字段 from 表 where 条件;
<code lang="mysql">select * from money where age=29;
说明:查询表中年龄为29的所有结果
基本语法五(结果集排序):select 字段 from 表 order 字段1 排序关键词,字段2 排序关键词,...,字段n 排序关键词
<code lang="mysql">select id,username,age,balance from money order by balance desc,age asc</code>
说明:在进行指定的条件查询之后产生结果之后集中排序使用order by,其中desc和asc是排序顺序中的关键字。desc表示降序排列,asc表示升序排列,如果不写关键字,默认升序排列。就如上面的就是以banlance为关键字,对id,username,age,balance这四个字段进行降序排序,然后以age为关键字,对这四个字段进行升序排序。但是,age关键字是第二关键字,所以如果balance已经把所有的都排好序之后,如果有balance相同的情况下,这些字段才会根据age排序,如果balance已经将结果排好,则后面的字段都不会生效了。
基本语法六(结果集限制):select 字段 from 表 limit 数量
<code lang="mysql">select id,name,balance from money limit 5</code>
说明:显示前五个用户
<code lang="mysql">select id,name,balance from money order by balance desc limit 5</code>
说明:排序后显示前五个用户
基本语法七(结果集区间选择):select 字段 from 表 limit 偏移量,数量
<code lang="mysql">select id,name,balance from money limit 0,3</code>
说明:从第一个记录开始取三个记录,第一条记录为0,通过这个思路,便可以完成分页的处理。
基本语法八(统计类函数使用):select 函数(字段) from 表
<code lang="mysql">select count(id) as zongshu from money</code>
说明:查询money表的id总数,常用的函数还有sum,max,min,avg等,而且利用as关键字可以给字段取别名。
基本语法九(分组):select * from 表 group by 字段
<code lang="mysql">select * from money group by province</code>
说明:将表按照province进行分组
基本语法十(在分组基础上进行统计):select * from 表 group by 字段 with rollup
<code lang="mysql">select count(province),province from money group by province with rollup</code>
说明:对分组的数再次进行统计,如对province进行分组,然后进行统计
基本语法十一(结果再过滤):select * from 表 group by 字段 having 条件
<code lang="mysql">select count as result,province from money group by province having result>2</code>
说明:将分组结果中的值按照条件显示出来,如上面的将结果中result的结果显示出来。
总结:select 字段集 [as 新字段集] from 表 [group by 字段集 [having 条件]] [where 条件][order 关键字集][limit 条件]
<b>MySQL多表联合查询</b>
有很多时候我们都要联合多个表来一起工作,比如在游戏中,人物是一个表,装备是另一个表,但是人物和装备表要联合使用才能发挥作用,而多表联合查询的本质就是:表连接。
当需要查询多个表中的字段时,就可以用表连接来实现。表连接分为内连接和外连接。
内连接:将两个表中存在联结关系的字段符合联结关系的那些记录形成记录集的联结。
基本语法一:select 表1.字段 [as 别名],...,表n.字段 from 表1 [别名语],表n where 条件
<code lang="mysql">select user.uid,user.name,goods.oid,goods.uid,goods.name from user,goods where user.uid=goods.uid
说明:查询商品表中哪些用户购买过商品,并将信息显示出来,其中uid是用来显示购买了商品的uid,而oid才是商品id。
基本语法二:select 表1.字段 [as 别名],表n.字段 from 表1 inner join 表n on 条件
<code lang="mysql">select user.uid,user.name,goods.oid,goods.uid,goods.name from user inner join goods on user.uid=goods.uid
外连接:会选出其他不匹配的记录,分为外左联结和外右联结。
基本语法:select 表1.字段 [as 别名],表n.字段 from 表1 left join 表n on 条件
<code lang="mysql">select * from user left join goods on user.uid=goods.uid</code>
说明:以左边为主,查询哪些用户未购买过商品,并将用户信息显示出来,左连接以左边为主,包含所有的左边表中的记录甚至是右边表中没有和它匹配的记录
子查询
基本语法:select 字段 from 表 where 字段 in(条件)
<code lang="mysql">select * from user where uid in(1,3,4)</code>
<code lang="mysql">select * from user where uid in(select uid from goods)</code>
说明:按照id查询指定用户 
记录联合
基本语法:select 语句1 union[all] select 语句2
<code lang="mysql">select uid from user union select uid from goods</code>
说明:将user表的uid和goods表的uid联合起来,即将商品表中的用户信息和用户表中的用户信息的结果组合在一起