前端需要掌握的 mysql 基础知识

常用的 mysql 的操作方法

1. 新增

这里新增phone,username,password三个参数,后面的?就是写几个, 对应的[phone, username, password]要和前面的顺序一致。

const sql2 = `INSERT INTO user(phone,username,password) VALUES(?,?,?)`;
const data2 = await query(sql2, [phone, username, password]);

2. 编辑

const sql = `update user set nickname='${nickname}', password='${password}'  where id=${id}`;

3. 删除

查询参数 如果是 int 的类型,即前端 number(数字)类型, 既可以不写' i d ′ , 直接 {id}', 直接 id′,直接{id}, 不要写引号也可以

 const sql = `delete from user where id = ${id}`;

4. 查询

4.1 模糊查询
    let whereSql = '1 = 1 ';
    if (username) {
      whereSql += `and username like '%${username}%'`;
    }
    const sql = `select * from user where ${whereSql}`;
4.2 精确查询
    let whereSql = '1 = 1 ';
    if (username) {
      whereSql += `and username = '${username}'`;
    }
    const sql = `select * from user where ${whereSql}`;
4.3 关联查询

关联查询解释:

user 表,包含(id,nickname)

product 表,包含(id,title,price,cover...)多个字段,

star 收藏表,包含(id,title,product_id,user_id),用户收藏了哪些产品,我们只要记录对应商品的 id 和记录人 id 即可,不需要将所有商品信息全部记录。

a. 查询收藏表数据

star.* 代表查询 star 表 所有字段,你可以写, start.id, star.title... 把每个字段都写出来也可以,或者你需要哪个,就写哪个 [关联 product 表查询]

const sql = `
            select star.*,  product.price from star
            inner join product on product.id = star.product_id
            `

b. 查询收藏表数据(根据时间排序)desc/asc 俩种排序规则 [关联 product 表查询]

const sql = `
            select star.*, product.price from star
            inner join product on
                  product.id = star.product_id
            order by star.create_time desc
            `

c. 关联 user 表 && product 表 查询, 这里最多建议关联三个表,不然影响查询性能

const sql = `
            select star.*, product.price from star
            inner join product on
                  product.id = star.product_id
            inner join user on
                  user.id = star.user_id
            order by star.create_time desc
            `

5. 分页查询

    let whereSql = '...'

    1. 计算总数量
    const totalSql = `SELECT COUNT(*) AS total FROM comment where ${whereSql}`;

    2. 计算分页
    const limit = `${(pageIndex - 1) * pageSize},${pageSize}`;

    3. 查询语句(order和limit顺序不能乱)
    const sql = `select comment.*, user.nickname, user.avatar from comment
              inner join
                  user on user.id = comment.user_id
              where
                  ${whereSql}
              order by create_time desc
              limit ${limit}
          `;

6. 查询更换字段名称

看上面 4.3 的 product 和 star 的表结构,就出现了,俩张表都有 title 字段
star.* 查询 star 表所有字段,其中有 title,product.title 也把 title 查询出来,这样重名子, 只会显示后者

解决方案 1:product.title as productTitle 这个 as 是关键字,mysql 的固定写,productTitle 自己随意定义名字

解决方案 2:product.title productTitle 不写也可以

const sql = `
            select star.*, product.title as productTitle, product.price from star
            inner join user on
                  user.id = star.user_id
            order by star.create_time desc
            `
相关推荐
kejijianwen1 小时前
JdbcTemplate常用方法一览AG网页参数绑定与数据寻址实操
服务器·数据库·oracle
编程零零七1 小时前
Python数据分析工具(三):pymssql的用法
开发语言·前端·数据库·python·oracle·数据分析·pymssql
(⊙o⊙)~哦3 小时前
JavaScript substring() 方法
前端
无心使然云中漫步4 小时前
GIS OGC之WMTS地图服务,通过Capabilities XML描述文档,获取matrixIds,origin,计算resolutions
前端·javascript
Bug缔造者4 小时前
Element-ui el-table 全局表格排序
前端·javascript·vue.js
高兴就好(石4 小时前
DB-GPT部署和试用
数据库·gpt
xnian_4 小时前
解决ruoyi-vue-pro-master框架引入报错,启动报错问题
前端·javascript·vue.js
这孩子叫逆5 小时前
6. 什么是MySQL的事务?如何在Java中使用Connection接口管理事务?
数据库·mysql
Karoku0665 小时前
【网站架构部署与优化】web服务与http协议
linux·运维·服务器·数据库·http·架构
码农郁郁久居人下5 小时前
Redis的配置与优化
数据库·redis·缓存