前端需要掌握的 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
            `
相关推荐
噼里啪啦啦.2 分钟前
Spring事务和事务传播机制
数据库·sql·spring
Lin86662 分钟前
Vue 3 + TypeScript 组件类型推断失败问题完整解决方案
前端
coding随想3 分钟前
从零开始:前端开发者的SEO优化入门与实战
前端
前端工作日常5 分钟前
我理解的JSBridge
前端
Au_ust5 分钟前
前端模块化
前端
顺丰同城前端技术团队5 分钟前
还不会用 Charles?最后一遍了啊!
前端
BUG收容所所长7 分钟前
二分查找的「左右为难」:如何优雅地找到数组中元素的首尾位置
前端·javascript·算法
彬师傅7 分钟前
geojson、csv、json 数据加载
前端
用户52709648744907 分钟前
🔥 我与 ESLint 的爱恨纠葛:从"这破玩意儿"到"真香警告"
前端
梨子同志8 分钟前
手动实现 JavaScript 的 call、apply 和 bind 方法
前端·javascript