常用sql语句

栏目: 数据库 · 发布时间: 6年前

内容简介:总结几个刚接触到的sql语句,以后慢慢完善。

总结几个刚接触到的 sql 语句,以后慢慢完善。

一、添加数据,insert语句

insert into table set columnName1 = value1,columnName2 = value2,…;

 const sq1 = 'insert into boke_articles set ?'//此处的问号表示来自于前端用户输入的数据,添加到boke_articles表中

 conn.query(sq1,body,(err,result) => {
      if(err) return res.send({msg:'发表文章失败',status:500})

      if(result.affectedRows !== 1) return res.send(({msg:'发表文章失败',status:501}))
 })

sq1 = 'insert into boke_articles set ?' 此处的问号表示来自于前端用户输入的数据,添加到boke_articles表中.

二、删除数据

delet语句

delet from table where ?

  • 问号表示 列名称 = 值 ,只删除指定的行

delete 语句用于删除表中的行。delete语句执行删除的过程是每次从表中删除一行,并且同时将该行的删除操作作为事务记录在日志中保存

delet from table //删除所有行

truncate语句

truncate (清空表中的数据):删除内容、释放空间但不删除定义(保留表的数据结构)。与drop不同的是,只是清空表数据而已。

drop (删除表)

删除内容和定义,释放空间。简单来说就是把整个表去掉.以后要新增数据是不可能的,除非新增一个表。

三、更新数据,update语句

update table set ? where id=?

  • 第一个问号表示 列名称 = 新值
  • 第二个问号表示 列名称 = 某值
const id = req.params.id
const sql = 'update heros set isdel=1 where id=?'

conn.query(sql,id,(err,result) => {
     if(err) return res.send({status:500, msg:err.message, data:null})
     res.send({status:200, msg:'ok', data:result})
})

sql = 'update heros set isdel=1 where id=?' 该语句表示想要把某个id的 isdel 属性的值更新为1.

四、获取数据

select * form table 在table中查找某些属性。

select pages.title,pages.star,pages.timedate,pages.visitNum from pages

如该条语句表示,在名称为pages的表中查找 pages.title,pages.star,pages.timedate,pages.visitNum 这些属性,结果如下:

常用sql语句

常用sql语句

limit语法

结合上面查询语句 select * form table limit m,n

  • 其中m是指记录开始的index,从0开始,表示第一条记录
  • n是指从第m+1条开始,取n条。
  • select pages.timedate from pages limit 4,5
  • 即取出第6条至第10条,共5条pages.timedate记录

常用sql语句

常用sql语句


以上所述就是小编给大家介绍的《常用sql语句》,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对 码农网 的支持!

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

Writing Apache Modules with Perl and C

Writing Apache Modules with Perl and C

Lincoln Stein、Doug MacEachern / O'Reilly Media, Inc. / 1999-03 / USD 39.95

Apache is the most popular Web server on the Internet because it is free, reliable, and extensible. The availability of the source code and the modular design of Apache makes it possible to extend Web......一起来看看 《Writing Apache Modules with Perl and C》 这本书的介绍吧!

RGB转16进制工具
RGB转16进制工具

RGB HEX 互转工具

UNIX 时间戳转换
UNIX 时间戳转换

UNIX 时间戳转换

HEX HSV 转换工具
HEX HSV 转换工具

HEX HSV 互换工具