mysql数据增删改查操作
2017-06-10 17:03
267 查看
1 插入数据
这条语句可以同时插入多条数据,若为所有字段插入数据,则column_list可以省略掉。 INSERT INTO table_name (column_list) VALUES (value_list1)[, (value_list2)...];
2 修改数据
UPDATE table_name SET column_name1 = value1, column_name2=value2,……, column_namen=valueN WHERE (condition);
3 删除数据
若无where 条件则删除所有数据 DELETE FROM table_name [WHERE condition] ;
4 查询数据
a 单表查询 语法规范: SELECT {*|<字段列表>} [ from <表1>, <表2>... [where <condition>] [group by <group by definition>] [having <expression> [{<operator> <expression>...}]] [order by <order by definition>] [limit [<offset>,] <row count>] ] 1. 查询所有字段 select * from mytable1; 2. 查询指定字段 select id, name from mytable1; 3. 筛选 select id, name from mytable1 where id=3; select id, name from mytable1 where id in (1, 2); select id, name from mytable1 where id between 1 and 3; //这里包括1-3之间的所有值 select id, name from mytable1 where name like 'b%'; select id, name from mytable1 where name like 'b__'; select id, name from mytable1 where name is not null; select id, name from mytable1 where id in (1, 2) and name is not null; select id, name from mytable1 where id=1 or id=2; //可以实现in功能,不过in效率更高 select distinct name from mytable1; //消除重复值 4. 排序,desc只对其前面的一个字段生效,如果要对每一个字段都降序,则每个字段后都要加desc 单列排序 select age from mytable1 order by age; 多列排序: select age, score from mytable1 order by score, age; 5. 分组 95aa select id, group_concat(name) as names from mytable1 group by id having count(name)>1; 统计分组数目: group by id with rollup;(该命令无法与order by一起使用) 6. 限制返回结果 limit 3,5; //第四行开始,共5行 7. 集合函数 count(), sum(), avg(),min(), max()等等,他们不包含NULL所在的行。 b 多表查询 1. 内连接查询 select mytable1.id, mytable1.name, mytable2.id, mytable2.name from mytable1 inner join mytable2 on mytable1.id=mytable2.id; 2. 外连接查询 左连接 select mytable1.id, mytable1.name, mytable2.id, mytable2.name from mytable1 left outer join mytable2 on mytable1.id=mytable2.id; 右连接 select mytable1.id, mytable1.name, mytable2.id, mytable2.name from mytable1 right outer join mytable2 on mytable1.id=mytable2.id; c 子查询 1. ANY, SOME为同义词,表示有一个满足即可 2. all 表示要满足所有条件才行 3. exits 存在为true, 否则为false 4. in 内层查询返回列表与外层比较 5. 带比较运算符的子查询 d 合并查询 union 重复的行被删除 union all 重复的行不被删除,效率比union高 e 使用正则表达式查询regexp select * from mytable1 where name regexp '^b*a$';
相关文章推荐
- MySQL学习笔记4:操作数据表中的记录(增删改查)
- nodejs操作mysql实现增删改查数据操作
- MySql 5.7 json数据格式 增删改查 操作 (不定时更新)
- MySQL---操作数据表基本操作(增删改查)
- MySQL从入门到放弃第五章:操作数据表中的记录(增删改查)
- mysql数据记录基本操作(对数据的增删查改)
- mysql操作数据 增删改查
- mysql-数据(记录)相关操作(增删改查)及权限管理
- Android+服务器上数据进行操作+mysql的增删改查
- mysql-数据基本操作(增删改查)
- java 操作mysql的创建数据库及数据的增删改查
- mysql数据增删改查操作
- java 操作mysql的创建数据库及数据的增删改查
- MYSQL的常用命令和增删改查语句和数据类型
- C#访问和操作MYSQL数据库
- mysql操作文本数据
- 类型化的数据集和非类型化数据完成增删改查的操作
- MySQL存储过程跨数据库操作同步数据
- python 操作mysql数据
- mysql常用增删改查等操作