【mysql】mysql中基本的增删改差操作案例
2012-11-21 00:28
309 查看
一、====sql语句(增删改查CURD)====
select * from 表名 where 条件 (/order by .../ limit ... /group by ...) =>(既有* 又有from)
delete form 表名 where 条件 =>(没有* 只有from)
update 表名 set 字段='值' where 条件 =>(既没有*又没有from)
insert into 表名(字段1,字段2,...) values('值1','值2',...) =>(特殊与上面三种格式不同)
联合查询
三种:左连接查询 left join on (=>以左表为基准)
右连接查询 right join on)(=>以右表为基准)
内连接查询 (inner) join on (=>两个表为基准,即两个表都存在的数据。)、
例如:
select * from tb_admin a (left/right/inner) join on tb_powergroup p on a.powergroup=p.powergroup;
二、===sql语句中常用函数===
1.length(字段名) 计算长度。
2.avg(字段名) 计算某一个字段的平均值。
3.max(字段名) / min(字段名) 求最大值 / 求最小值
4.concat(字段名1,字段名2) 将两个字符串连接起来。
5.md5() 加密。
例子:
1、查询用户表tb_user中姓名是3个字并且姓王,年龄大于30,性别男。
select * from tb_user where username like '王%' and length(username)=6 and age>30 and sex=0; (若是gbk编码,一个汉字则占两个字节。若是utf-8,一个汉字占3个字节。)
(用到1模糊查询like 。2、获取某个字段长度length()函数。3、where后的每个条件之间用and连接)
2、把商品表tb_goods中的 vip_price价格改为本站价格shop_price和市场价格market_price的平均值的80%。
update tb_goods set vip_price=(shop_price+market_price)/2*0.8;
(用到1、浮点型数据之间的运算。2、sql语句中不能用80%,因为%有特定含义,需写为0.8即可。)
3、把用户表tb_user中pwdquestion的值改为pwdanswer的值加pwdquestion的值。
update tb_user set pwdquestion=concat(pwdanswer,pwdquestion);
(用到字符串数据类型之间的运算用concat()函数)
4、查询用户表tb_user中年龄最大用户的用户名和email。
select username,email from tb_user where age=(select max(age) from tb_user);
(用到1、嵌套查询。2、获取最大值max()函数因为它是组函数所以后面必须对age进行分组。)
5、查询商品表tb_goods中shop_price在4000到5000之间,并且是诺基亚品牌的商品。
第一种方法:
select * from tb_goods where shop_price between 4000 and 5000 and goods_brandid=(select id from tb_brand where brand_name='诺基亚');
第二种方法:(联合查询的方法=> 安全性高,执行效率快)
select * from tb_goods g join tb_brand b on g.goods_brandid=b.id where g.shop_price between 4000 and 5000 and b.brand_name='诺基亚';
select * from 表名 where 条件 (/order by .../ limit ... /group by ...) =>(既有* 又有from)
delete form 表名 where 条件 =>(没有* 只有from)
update 表名 set 字段='值' where 条件 =>(既没有*又没有from)
insert into 表名(字段1,字段2,...) values('值1','值2',...) =>(特殊与上面三种格式不同)
联合查询
三种:左连接查询 left join on (=>以左表为基准)
右连接查询 right join on)(=>以右表为基准)
内连接查询 (inner) join on (=>两个表为基准,即两个表都存在的数据。)、
例如:
select * from tb_admin a (left/right/inner) join on tb_powergroup p on a.powergroup=p.powergroup;
二、===sql语句中常用函数===
1.length(字段名) 计算长度。
2.avg(字段名) 计算某一个字段的平均值。
3.max(字段名) / min(字段名) 求最大值 / 求最小值
4.concat(字段名1,字段名2) 将两个字符串连接起来。
5.md5() 加密。
例子:
1、查询用户表tb_user中姓名是3个字并且姓王,年龄大于30,性别男。
select * from tb_user where username like '王%' and length(username)=6 and age>30 and sex=0; (若是gbk编码,一个汉字则占两个字节。若是utf-8,一个汉字占3个字节。)
(用到1模糊查询like 。2、获取某个字段长度length()函数。3、where后的每个条件之间用and连接)
2、把商品表tb_goods中的 vip_price价格改为本站价格shop_price和市场价格market_price的平均值的80%。
update tb_goods set vip_price=(shop_price+market_price)/2*0.8;
(用到1、浮点型数据之间的运算。2、sql语句中不能用80%,因为%有特定含义,需写为0.8即可。)
3、把用户表tb_user中pwdquestion的值改为pwdanswer的值加pwdquestion的值。
update tb_user set pwdquestion=concat(pwdanswer,pwdquestion);
(用到字符串数据类型之间的运算用concat()函数)
4、查询用户表tb_user中年龄最大用户的用户名和email。
select username,email from tb_user where age=(select max(age) from tb_user);
(用到1、嵌套查询。2、获取最大值max()函数因为它是组函数所以后面必须对age进行分组。)
5、查询商品表tb_goods中shop_price在4000到5000之间,并且是诺基亚品牌的商品。
第一种方法:
select * from tb_goods where shop_price between 4000 and 5000 and goods_brandid=(select id from tb_brand where brand_name='诺基亚');
第二种方法:(联合查询的方法=> 安全性高,执行效率快)
select * from tb_goods g join tb_brand b on g.goods_brandid=b.id where g.shop_price between 4000 and 5000 and b.brand_name='诺基亚';
相关文章推荐
- MySql学习记录1-mysql的基本操作之增删改
- mysql用户基本操作
- mysql 数据表的基本操作
- mysql常用基本操作
- MySQL之终端(Terminal)管理数据库、数据表、数据的基本操作
- java使用JDBC连接mysql并且进行批量增删改操作
- mysql启动binlog以及基本操作
- MySQL基本操作
- php mysql基本操作
- MySQL学习笔记 2 - 基本操作
- MySQL 的基本操作
- 实例讲解mysql入门基本操作语句
- Ubuntu 操作mysql 基本知识
- 035--MySQL基本操作
- mysql的基本操作
- MySQL基本操作
- PHP初学-mysql的基本操作
- mysql 基本操作
- Mysql的基本权限命令操作-菜鸟的进击之路
- java-mysql基本操作笔记2