ruby数据库 update update_all delete_all方法应用

阅读更多

1.数据库update查询

  update 表名称 set 字段名+where+字段名

例如 posts表

id title author
1 foolish 猪八戒
2 stupid 孙悟空

 update posts set title="clever" where author="孙悟空"

结果如下:

id title author
1 foolish 猪八戒
2 clever 孙悟空

ruby中的应用方法如:

Post.update("title="clever","author='孙悟空'")

update_all用法同上,

2.delete数据库中用法

delete 表名(删除表)delete*from 表名(删除表)

delete * from 表名 where+ 条件

例如:

delete *from posts where author="猪八戒"

结果如下

id title author
1 clever 孙悟空

ruby中的用法

Post.delete(条件)

Post.delete_all(条件)

用法与update及其相似

 

你可能感兴趣的:(sql,update,update_all,delete,delete_all)