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及其相似

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章