前端需要掌握的 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
            `
相关推荐
2601_9637713718 分钟前
WordPress SEO Guide: Boost Rankings Without Technical Jargon
前端·php
李顿波19 分钟前
Chrome 扩展程序 MV3 架构组件概览
前端·chrome·浏览器插件
AI-好学者24 分钟前
阶段二-Cypher查询语言详解
数据库·rag·graphrag
KingCodeHan34 分钟前
ESLint 完全指南:从入门到精通
前端
龙仔72535 分钟前
SQL Server 批量多库只读账号一键脚本(带逐行注释完整脚本 + 分段逐句详细解释)
服务器·数据库·sql·sqlserver·dba
GHL2842710901 小时前
安装chrome浏览器
前端·chrome
谙忆10241 小时前
WebCodecs 实战:用 ImageDecoder 和 VideoFrame 在浏览器里做硬件加速的帧处理
前端
JerrySir1 小时前
密码没泄露,2FA 也没失效:Chrome 146 为什么还要让 Cookie“搬不走”?
前端
小蚂蚁i1 小时前
React Hooks 原理深度解析:从会用到真正懂
前端·react.js