前端需要掌握的 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
            `
相关推荐
德育处主任Pro3 分钟前
前端玩转大模型,DeepSeek-R1 蒸馏 Llama 模型的 Bedrock 部署
前端·llama
Jedi Hongbin30 分钟前
Three.js NodeMaterial 节点材质系统文档
前端·javascript·three.js·nodematerial
CS Beginner40 分钟前
【Linux】Mysql的基本文件组成和配置
linux·运维·mysql
前端小马40 分钟前
前后端Long类型ID精度丢失问题
java·前端·javascript·后端
用户1456775610371 小时前
干净的图片批量处理,处理速度飞快
前端
点灯小铭1 小时前
基于单片机的自动存包柜设计
数据库·单片机·mongodb·毕业设计·课程设计
用户1456775610371 小时前
亲测好用!简单实用的图片尺寸调整工具
前端
索西引擎1 小时前
npm、yarn、pnpm
前端·npm·node.js
失散131 小时前
软件设计师——09 数据库技术基础
数据库·软考·软件设计师
养生技术人1 小时前
Oracle OCP认证考试题目详解082系列第53题
数据库·sql·oracle·database·开闭原则·ocp