mysql数据增删改查操作
2016-02-22 20:50
656 查看
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. 分组 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 insert和前台显示乱码
- mysql阅读笔记四
- MySQL创建帐号/修改权限不生效
- mySQL相关函数的使用
- MySQL ROLLUP和CUBE区别
- Mysql 分区 分表相关总结之方案选择
- Mysql第四天 数据库设计
- Mysql中用表a中数据更新另一表b中数据的同时a的数据被修改
- MYSQL的常用命令和增删改查语句和数据类型
- MYSQL表类型MyISAM和InnoDB区别
- mysql中的数据表操作
- 读取nagios数据库至MySQL
- MYSQL使用mysqldump导出某个表的部分数据
- MySQL字符集操作
- MySQL存储过程详解
- mysql root密码修改
- Mysql事物锁等待超时 Lock wait timeout exceeded; try restarting transaction
- Mysql事物锁等待超时 Lock wait timeout exceeded; try restarting transaction
- Mysql事物锁等待超时 Lock wait timeout exceeded; try restarting transaction
- MySQL主从复制、半同步复制和主主复制