简介 目录 评价 推荐
  • 慕设计0359233 2025-03-22

    1、插入数据

    insert into 表(字段1,字段2) values(字段1值1,字段2值1),(字段1值2,字段2值2)

     

    2、删除数据

    delete from 表:删除所有数据

    delete from 表 where 条件:删除指定的数据


    3、更新数据

    update 表 set 字段=值 :修改表中所有这个字段的值

    update 表 set 字段1=值1,字段2=值2 where 条件:修改指定数据的值

    0赞 · 0采集
  • PENG121 2025-03-12

    https://img1.sycdn.imooc.com/8c5b3b6709d11d9411340518.jpg

    https://img1.sycdn.imooc.com/4491396709d11e4b07560415.jpg

    https://img1.sycdn.imooc.com/e614ac6709d11e6210860451.jpg

    https://img1.sycdn.imooc.com/cf65cf6709d11e9f09850459.jpg

    https://img1.sycdn.imooc.com/b0ec076709d11ee908860454.jpg

    https://img1.sycdn.imooc.com/145c4e6709d11f0211740324.jpg

    https://img1.sycdn.imooc.com/ac1fcf6709d11f1f09800510.jpg

    https://img1.sycdn.imooc.com/4ce2d96709d11f4108170515.jpg

    https://img1.sycdn.imooc.com/4cd2956709d11f6110250425.jpg

    https://img1.sycdn.imooc.com/b554eb6709d11f8610810365.jpg

    https://img1.sycdn.imooc.com/e16ac46709d11fd410650343.jpg



    https://img1.sycdn.imooc.com/30be5f6709d1200211170366.jpg

    https://img1.sycdn.imooc.com/b3ad3d6709d1203210860477.jpg

    https://img1.sycdn.imooc.com/4c1e126709d1205410780338.jpg

    https://img1.sycdn.imooc.com/ffb25b6709d1207610250448.jpg

    https://img1.sycdn.imooc.com/ead4456709d1210c11770600.jpg


    https://img1.sycdn.imooc.com/ec51786709d1212c10560590.jpg

    https://img1.sycdn.imooc.com/c369cf6709d1215a09590624.jpg

    https://img1.sycdn.imooc.com/e1cb056709d121a305810516.jpg

    https://img1.sycdn.imooc.com/d90bee6709d121ca05750471.jpg

    https://img1.sycdn.imooc.com/d4ce5d6709d121dd10230482.jpg




    MySQL总结

































    0赞 · 0采集
  • PENG121 2025-03-11

    在Node.js中使用MySQL

    • 在Node.js中连接数据库

    • 使用数据库连接池

    • 使用Promise

    • 使用预处理

    0赞 · 0采集
  • PENG121 2025-03-10

    https://img1.sycdn.imooc.com/37e56d6709ceeb0915230766.jpg

    在执行sql语句时,有时会遇到大量结构相同仅部分变量不同的语句,直接执行这些语句消耗的时间是巨大的,而预处理则是为了这种情况准备的语法

    一种减轻服务器压力的技术

    传统SQL语句处理流程:

    --在客户端准备sql语句

    select * from student where id =1

    --发送sql语句到mysql服务器

    --mysql服务器对sql语句进行解析-编译-执行

    --将执行结果返回给客户端

    传统的弊端:

    --即使多次传递的语句大部分内容都是相同的,每次还是要重复传递

    --即使多次传递的语句大部分内容都是相同的,每次执行之前还是要先解析、编译之后才能执行


    预处理的处理流程:

    --在客户端准备预处理的SQL语句

    --发送预处理SQL语句进行解析-编译,但不执行

    --在客户端准备相关数据

    --MySQL服务器对数据和预处理SQL编译,然后执行该SQL语句

    --服务器将执行结果返回给客户端

    --预处理优点:只对SQL语句进行了一次解析;重复内容大大减少(网络传输更快)


    如何使用预处理

    -在客户端准备预处理的SQL语句

    prepare  ppstmt from 'select * from student where id = ?;';

    --发送预处理SQL语句进行解析-编译,但不执行

    --在客户端准备相关数据

    set @id = 1;

    --MySQL服务器对数据和预处理SQL编译,然后执行该SQL语句

    execute ppstmt using @id

    --服务器将执行结果返回给客户端

    https://img1.sycdn.imooc.com/5ae7c96709cef1d503630085.jpg

    0赞 · 0采集
  • PENG121 2025-03-10

    --多表查询

    直接查询:只需要在单表查询的基础上增加一张表即可,返回的结果是多张表的表数据个数的乘积,会有冗余数据

    select * from student,class

    select * from student,class where student.class_id = class.id

    https://img1.sycdn.imooc.com/51c1e46709ceacb203740096.jpg

    https://img1.sycdn.imooc.com/ed4a376709ceacbf04840153.jpg

    https://img1.sycdn.imooc.com/78d2306709ceaccf08480144.jpg

    连接查询:

    内连接查询(INNER JOIN 或 JOIN)

    select * from student join class on student.class_id = class.id;

    https://img1.sycdn.imooc.com/819cc56709cead6107900124.jpg

    select student.id class.name  from student join class on student.class_id = class.id;

    https://img1.sycdn.imooc.com/e70fc76709ceadd201850132.jpg

    select stu .id cls.name  from student stu join class cls on stu .class_id = cls.id;   --- 取别名

    https://img1.sycdn.imooc.com/b9ffe16709ceae1802480148.jpg


    外连接查询: 

    左外连接查询(LEFT OUTER JOIN 或 LEFT JOIN)

    左边的表是不看条件的,无论条件是否满足,都会返回左边中的所有数据,只有右边的表会看条件,对于右表,只有满足条件,才会返回

    select * from student left join class on student.class_id = class.id;

    https://img1.sycdn.imooc.com/5311c96709ceaeae07850151.jpg

    右外连接查询(LEFT OUTER JOIN 或 LEFT JOIN)

    右边的表是不看条件的,无论条件是否满足,都会返回右边中的所有数据,只有左边的表会看条件,对于左表,只有满足条件,才会返回

    select * from student right join class on student.class_id = class.id;

    https://img1.sycdn.imooc.com/2da5ef6709ceaf9508060137.jpg

    UNION 查询

    在纵向上将多张表的查询结果拼接起来返回  注意:必须保证多张表查询的字段个数一致(id,name)

    select id,name from student union select id,name,from class

    https://img1.sycdn.imooc.com/f9f1896709ceafe003820203.jpg


    子查询:将一个查询语句查询的结果作为另外一张表的查询条件来使用

    select class_id from student where id=3

    select class_id from student where id>=3

    select name from class where id = (select class_id from student where id=3)

    select name from class where id = (select class_id from student where id>=3) -- 报错

    select name from class where id in (select class_id from student where id>=3) -- 正确


    https://img1.sycdn.imooc.com/ffed296709ceb10b07210119.jpg

    0赞 · 0采集
  • PENG121 2025-03-10

    --分组查询 GROUP BY: 在对数据进行分组的时候,select后面必须是分组字段或者聚合函数

    select * from student;

    select class from student group by class;

    https://img1.sycdn.imooc.com/f365b96709ce9be503510129.jpg

    select class, avg(age) from student group by class;

    https://img1.sycdn.imooc.com/da432f6709ce9d3701940106.jpg

    select gender, avg(age) avgAge from student group by gender;

    https://img1.sycdn.imooc.com/8aa22a6709ce9d8602660104.jpg

    --HAVING条件查询:  WHERE 是取数据表中查询符合条件的数据返回结果集,HAVING是取结果集中查询符合条件的数据,可以对分组之后查询到的结果进行筛选

    select class , avg(age) from student group by class;

    https://img1.sycdn.imooc.com/20bd7d6709ce9ef202340129.jpg

    select class , avg(age) from student group by class where avg(age) <= 19 ; --报错

    select class , avg(age) avg from student group by class having avg(age) <= 19 ; --正确  在结果集中筛选

    https://img1.sycdn.imooc.com/44e7326709ce9ecd02250081.jpg

    0赞 · 0采集
  • PENG121 2025-03-10

    -- 聚合函数: 对表中的数据进行统计和计算,一般结合分组(GROUP BY)来使用,用于统计和计算分组数据

    COUNT() 计算查询到的多少条数据

    SUM() 计算查询结果中所有指定字段的和

    AVG() 计算查询结果中所有指定字段的平均值

    MAX() 求查询结果中指定字段的最大值

    MIN() 求查询结果中指定字段的最小值


    select * from student;

    select count(*) from student;

    select count(id) from student;


    select sum(age) from student;

    select avg(age) from student;

    select max(age) from student;

    select min(age) from student;


    --给查询出来的字段起别名  两种写法同效果

    select count(*) as total from student;  

    select count(*) total from student;

    https://img1.sycdn.imooc.com/05bd2f6709ce9ac902540048.jpg

    0赞 · 0采集
  • PENG121 2025-03-10

    ---排序

    select * from student order by age asc; -- 默认就是升序 ascending

    select * from student order by age desc; -- 默认就是降序 descending

    select * from student order by age desc , id asc; -- 先按照年龄降序,再按id升序


    --分页查询

    limit -- 一次查询的条数 offset 偏移量

    limit --  偏移量,一次查询的条数

    select * from student limit 10 offset 0;

    select * from student limit 10 offset 10;

    select * from student limit 10 offset 20;


    select * from student limit 0, 10;  偏移量0查询10条

    0赞 · 0采集
  • PENG121 2025-03-10

    https://img1.sycdn.imooc.com/049a526709ce89c410220519.jpg

    https://img1.sycdn.imooc.com/b52b5b6709ce8b2709380409.jpg

    --1、 基础查询

    select id, name from student; -- 查询指定的字段

    select * from student -- 查询所有字段

    https://img1.sycdn.imooc.com/b62c366709ce8c5806420448.jpg

    --2、WHERE 条件查询

    https://img1.sycdn.imooc.com/6411276709ce917104580139.jpg

    select * from student  where id=4;

    select * from student  where id>=4;

    select * from student  where id!=4;


    select * from student  where age >=19 and  gender ='男';

    select * from student  where age >=19 or gender ='男';

    select * from student  where not age >=19;


    select * from student  where id in(1, 2, 4); -- 范围限定在指定的值中

    select * from student  where id between 1 and 4; -- 在一个范围内,包含边界



    select * from student  where  class  is null; -- 查 class 为null的数据

    select * from student  where  class  is not null; -- 查 class 不为null的数据


    select * from student  where  name like '张%';   ----  %百分号是匹配任意个任意字符

    select * from student  where  name like '张_';   ----    _下划线匹配一个任意字符

    select * from student  where  name like '%三';   ----    查询‘三’结尾,前面任意个任意字符

    select * from student  where  name like '%三%';   ----   查询包含‘三' 的数据

    0赞 · 0采集
  • PENG121 2025-03-10

    https://img1.sycdn.imooc.com/df39736709ce87a406000186.jpg

    -- 1、插入数据(增)

    insert into user (username) values('Alex');

    insert into user (username,gender) values ('张三', '男');

    insert into user (username,gender) values ('张三', '男'), ('李四', '男');


    ---2、删除数据(删)

    delete from user; --- 删除所有的数据

    delete from user where id=6;


    ---3、更新数据(改)

    update user set username='赵六'; --- 修改所有的

    update user set username='Alex', gender='男' where id=5; -- 修改id=5指定的数据

    0赞 · 0采集
  • daozuddt 2024-12-19
    只要多表查询,就需要建立外键。外键可以建立在关联表的任意一个中
    0赞 · 0采集
  • daozuddt 2024-12-17
    join ...on...
    group by ...having...
    0赞 · 0采集
  • daozuddt 2024-12-17
    数据库连接池
    npm启动数据库
    0赞 · 0采集
  • daozuddt 2024-12-17
    prepare 名称 from 'sql预处理语句'
    set
    exue
    0赞 · 0采集
  • daozuddt 2024-12-16
    join可以与where一起使用
    0赞 · 0采集
  • daozuddt 2024-12-16
    多对多查询,用到中间关系表,关联时候一条语句用到两次join
    0赞 · 0采集
  • daozuddt 2024-12-16
    join后面的条件用on,不用where
    0赞 · 0采集
  • daozuddt 2024-12-15
    注意使用left,right join,使用了左边和右边,的单词就会将其一侧的表格范围放宽,指约束反向测的条件查询。

    Where的条件查询只针对数据表有效,而select语句查询出来的内容是,结果集并不是表数据,所以对select查询之后的结果再进行条件划分的话,就不能用where
    0赞 · 0采集
  • daozuddt 2024-12-15
    需要分组的字段写在select语句要素中的首个
    0赞 · 0采集
  • 哔哩吧啦就 2024-11-04

    预处理https://img1.sycdn.imooc.com/67286c190001e48109600126.jpg

    https://img1.sycdn.imooc.com/67286c2100017db105300154.jpg 

    https://img1.sycdn.imooc.com/67286c290001629509600098.jpg

    https://img1.sycdn.imooc.com/67286c4100014b9309900262.jpg

    https://img1.sycdn.imooc.com/67286c7d0001400205240114.jpg

    https://img1.sycdn.imooc.com/67286c9700018ecd09140243.jpg

    https://img1.sycdn.imooc.com/67286d270001001d10340355.jpg

    0赞 · 0采集
  • 慕粉0006422813 2024-09-27

    create table if not EXISTS user (

    id int UNSIGNED PRIMARY KEY auto_increment,

    username VARCHAR(20),

    gender ENUM('男','女', '保密') DEFAULT '保密'

    );

    show TABLES;

    DESC user;


    -- 1、插入

    insert INTO user (username) VALUES ('Alex');

    insert INTO user (username, gender) VALUES ('张三', '男');

    insert INTO user (username, gender) VALUES ('李四', '男'), ('王五', '男');


    -- 2、删除

    DELETE from user WHERE id = 2;


    -- 3、更新

    update user set username='mali', gender='女' where user.id = 1;


    create table if not EXISTS student (

    id int UNSIGNED PRIMARY KEY auto_increment,

    name VARCHAR(20) not NULL,

    gender ENUM('男','女', '保密') DEFAULT '保密',

    class VARCHAR(20),

    age INT NOT NULL,

    createAt TIMESTAMP DEFAULT CURRENT_TIMESTAMP,

    updateAt TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP

    );


    INSERT INTO student (name, age, gender, class) values ('王五',21,'女','二班');

    INSERT INTO student (name, age, gender, class) values ('赵柳',22,'男','一班');

    INSERT INTO student (name, age, gender, class) values ('孙琦',20,'女','三班');

    INSERT INTO student (name, age, gender) values ('粥吧',19,'男');

    INSERT INTO student (name, age, gender, class) values ('及时',18,'女','二班');

    INSERT INTO student (name, age, gender, class) values ('李盼',18,'女','一班');

    INSERT INTO student (name, age, gender) values ('冯吉', 19,'男');


    -- 1. 基础查询

    SELECT id,name from student;

    SELECT * from student;


    -- 2. where条件查询

    -- >、>=、<、<=、=、!=

    -- AND、OR、NOT

    -- IN(值1,值2,...)、BETWEEN...AND

    -- IS NULL、IS NOT NULL

    -- LIKE 模糊查询

    SELECT * from student WHERE student.id=4;

    SELECT * from student WHERE student.age>=19 AND gender='男';

    SELECT * from student WHERE student.age IN(19,20,21);

    SELECT * from student WHERE student.age BETWEEN 19 AND 21;

    SELECT * from student WHERE student.class IS NULL;

    SELECT * from student WHERE student.name LIKE '王%'; -- % 表示任意个数的任意字符

    SELECT * from student WHERE student.name LIKE '王_'; -- _ 表示一个任意字符

    SELECT * from student WHERE student.name LIKE '%五%'; -- _ 表示一个任意字符


    -- 3.排序

    SELECT * FROM student ORDER BY age asc; -- 默认就是升序 ascending

    SELECT * FROM student ORDER BY age desc; -- 降序 descending

    SELECT * FROM student ORDER BY age desc, id asc; -- 先按照年龄降序,如果年龄相同则按照id升序排列


    -- 4.分页查询

    -- LIMIT 一次查询的条数 OFFSET 偏移量

    -- LIMIT 偏移量,一次查询的条数

    SELECT * FROM student LIMIT 2 OFFSET 0;

    SELECT * FROM student LIMIT 2 OFFSET 2;

    SELECT * FROM student LIMIT 2 OFFSET 4;

    SELECT * FROM student LIMIT 0, 2;


    -- 5.聚合函数

    -- 例如函数 SELECT DATABASE() 非聚合函数;

    -- 对表中的数据进行统计和计算,一般结合分组(GROUP BY)来使用,用于统计和计算分组数据

    -- COUNT() 计算查询到了多少条数据

    -- SUM() 计算查询结果中所有指定字段的和

    -- AVG() 计算查询结果中所有指定字段的平均值

    -- MAX() 查询结果中指定字段的最大值

    -- MIN() 查询结果中指定字段的最小值

    SELECT * FROM student;

    SELECT COUNT(*) FROM student;

    SELECT SUM(age) FROM student;

    SELECT AVG(age) FROM student;

    SELECT MAX(age) FROM student;

    SELECT MIN(age) FROM student;

    -- 给查询出来的字段起别名

    SELECT COUNT(*) totalRecord FROM student;


    -- 6.分组查询 GROUP BY

    SELECT * FROM student;

    -- 在对数据进行分组的时候, SELECT 后面必须是分组字段或者聚合函数

    -- SELECT * FROM student GROUP BY class; -- 错误案例

    SELECT class FROM student GROUP BY class;

    SELECT class, avg(age) FROM student GROUP BY class;

    SELECT gender,AVG(age) avgAge FROM student GROUP BY gender;


    -- 7.HAVING 条件查询

    -- WHERE 是去数据表中查询符合条件的数据返回结果集

    -- HAVING 是去结果集中查询符合条件的数据,可以对分组之后查询到的结果进行筛选

    -- SELECT class,AVG(age) avgAge FROM student GROUP BY class WHERE avgAge<=19.5; -- where不行会报错

    SELECT class,AVG(age) avgAge FROM student GROUP BY class HAVING avgAge<=19.5;


    -- 8. 多表查询

    -- 8.0.准备

    create table if not EXISTS class ( -- 班级表

    id TINYINT UNSIGNED PRIMARY KEY auto_increment,

    name VARCHAR(20) NOT NULL,

    `desc` VARCHAR(255),

    createdAt TIMESTAMP DEFAULT CURRENT_TIMESTAMP,

    undateAt TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP

    );

    INSERT into class (name, `desc`) VALUES('一班', '火箭班');

    INSERT into class (name, `desc`) VALUES('二班', '飞机班');

    INSERT into class (name, `desc`) VALUES('三班', '高铁班');

    INSERT into class (name, `desc`) VALUES('四班', '汽车班');


    CREATE TABLE IF NOT EXISTS student ( -- 学生表

    id INT UNSIGNED PRIMARY KEY auto_increment,

    name VARCHAR(20) NOT NULL,

    age TINYINT UNSIGNED NOT NULL,

    gender ENUM('男', '女', '未知') DEFAULT '未知',

    class_id TINYINT UNSIGNED,

    createdAt TIMESTAMP DEFAULT CURRENT_TIMESTAMP,

    undateAt TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,

    FOREIGN KEY(class_id) REFERENCES class(id) ON UPDATE CASCADE ON DELETE SET NULL

    );

    INSERT INTO student(name, age,gender, class_id) values('张三', 20, '男', 1);

    INSERT INTO student(name, age,gender, class_id) values('李四', 21, '女', 3);

    INSERT INTO student(name, age,gender, class_id) values('王五', 19, '女', 2);

    INSERT INTO student(name, age,gender, class_id) values('赵六', 16, '男', 1);

    INSERT INTO student(name, age,gender, class_id) values('宋七', 19, '女', 2);

    INSERT INTO student(name, age,gender) values('康良', 19, '女');


    -- 8.10 直接查询

    -- 秩序在单表查询基础上增加一张表即可,返回的结果是多张表数据局条数的乘积

    SELECT * FROM student, class WHERE student.class_id = class.id;


    -- 8.2 连接查询

    -- 8.2.1 内连接(INNER JOIN 或 JOIN)

    -- 内连接的查询结果和和直接查询的结果是一样的

    SELECT * FROM student JOIN class; -- SELECT * FROM student,class;

    SELECT * FROM student JOIN class ON student.class_id = class.id;

    SELECT student.id id,student.name,class.name,class.id cid FROM student JOIN class ON student.class_id = class.id;


    -- 8.2.2 外连接查询

    -- 8.2.2.1 左外连接查询(LEFT OUTER JOIN 或 LEFT JOIN)

    SELECT * FROM student LEFT JOIN class on student.class_id = class.id;


    -- 8.2.2.2 右外连接查询 (RIGHT OUTER JOIN 或 RIGHT JOIN)

    SELECT * FROM student RIGHT JOIN class on student.class_id = class.id;


    -- 8.3 UNION 查询

    -- 在纵向上将多张表的查询结果拼接起来返回

    -- 必须保证查询的多张表的字段是一致的

    SELECT id,name FROM student UNION SELECT id,name from class;


    -- 8.4 子查询

    -- 8.4.1 将一个查询语句查询的结果作为另一个查询语句的条件来使用

    SELECT name, id from class WHERE id = (SELECT class_id FROM student WHERE id = 3)

    SELECT name, id from class WHERE id IN (SELECT class_id FROM student WHERE id >= 3)

    -- 8.4.2 讲一个查询语句的结果作为另一个查询语句的表来使用

    SELECT name from class WHERE id >= 2;

    SELECT * FROM (SELECT name from class WHERE id >= 2) t; -- 必须给子查询起个别名

    0赞 · 0采集
  • 焦小灰 2024-03-06

    select class,avg(age) from student group by class;

    0赞 · 0采集
  • weixin_慕瓜0054915 2024-01-24

    111111biao

    0赞 · 1采集
数据加载中...
开始学习 免费