SQL 语句

218 阅读1分钟

1.语句

  • 查询 (select)
  • 插入 (insert into)
  • 更新 (update)
  • 删除 (delete)

2.条件语句

  • where
  • and或or运算符
  • order by 排序
  • count(*)函数

3.查询语句

  • select * from users 查询所有数据 微信图片_20220313184529.png
  • select username,password from users 查询表中的username,password两列数据 微信图片_20220313184524.png
  • insert into users (username,password) values ('小丽','123456')插入数据
  • update users set username ='小刘' where id=4 更新数据
  • update users set username ='小五',password=11111 where id=4 更新某一条数据
  • delete from users where id=4 删除数据
  • select * from users order by id desc/asc(降序/升序默认)
  • select count(*) from users 查询数据总数
  • select count(*) as total from users 起别名

4.where子运算符

微信图片_20220313185911.png

5.在项目中如何操作数据库

微信图片_20220313192337.png

6.安装与配置mysql

  • 创建文件夹
  • 初始化 npm init -y
  • npm i mysql
  • 创建一个js文件,写入如下代码 微信图片_20220313192731.png
  • 测试是否成功 微信图片_20220313192951.png

7.操作数据库

  • 查询数据 微信图片_20220313221101.png

  • 插入数据 微信图片_20220313221442.png

简化写法 微信图片_20220313221855.png

  • 更新数据

微信图片_20220313222035.png 简化写法

微信图片_20220313222136.png

  • 删除数据

微信图片_20220313222300.png 标记删除 使用delete会真正把用户删除,为了避免,使用标记清除的方式删除 修改状态

微信图片_20220313222552.png