前端需要掌握的 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
            `
相关推荐
C_心欲无痕21 小时前
前端如何实现 [记住密码] 功能
前端
正在走向自律1 天前
金仓数据库KingbaseES中级语法详解与实践指南
数据库·oracle·kingbasees·金仓数据库·信创改造
Gofarlic_oms11 天前
Windchill用户登录与模块访问失败问题排查与许可证诊断
大数据·运维·网络·数据库·人工智能
我是小疯子661 天前
Python变量赋值陷阱:浅拷贝VS深拷贝
java·服务器·数据库
qq_316837751 天前
uni.chooseMedia 读取base64 或 二进制
开发语言·前端·javascript
Zoey的笔记本1 天前
2026告别僵化工作流:支持自定义字段的看板工具选型与部署指南
大数据·前端·数据库
静听山水1 天前
docker安装starrocks
数据库
小二·1 天前
Python Web 开发进阶实战:混沌工程初探 —— 主动注入故障,构建高韧性系统
开发语言·前端·python
gis开发1 天前
【无标题】
java·前端·javascript
小二·1 天前
Python Web 开发进阶实战:低代码平台集成 —— 可视化表单构建器 + 工作流引擎实战
前端·python·低代码