jiefancis / blogs

个人博客,学习笔记(issues)
1 stars 0 forks source link

mysql多表查询打卡 #9

Open jiefancis opened 3 years ago

jiefancis commented 3 years ago

mysql单表

查找

增 插入

insert into tableName (fieldName1, fieldName2, fieldName3) values(value1, value2, value3) 避免重复插入?

改 修改

update users set name ='', pass='',avator='' where id = ; 修改多条数据呢?

delete from users where id = ''

排序

select * from users where name like '%张%' order by fieldName limit page,pageSize

条件语句

关键字

mysql多表

多表查询谓词

多表关联查询

子查询 (也叫嵌套查询)(in exits from)

子查询 当一个查询是另一个查询的条件时,称之为子查询

- in 简单嵌套查询

    分析: 先查询张三的id 再根据他的id 查询订单

    select id  from user where username = '张三';
    select * from orders where user_id = 3;--结果为3

    以上执行两部太麻烦,两者和2 为1
    select * from orders where user_id = ( select id  from user where username = '张三');

    - 一个字段多个值如何查询
    select * from posts where id in (1,2,3)
    select * from posts where id = 1 or id = 2 or id =3
    select * from posts where id in (select id from users where name like '%张三%')

- in 复杂嵌套查询
    - 多表之间的嵌套查询

    ```
    test_expression[NOT] IN{
        subquery (参数说明:test_expression指SQL表达式,subquery包含某结果集的子查询)
    }
    ```

    多表嵌套查询的原理:无论是多少张表进行嵌套,表与表之间一定存在某种关联,通过WHERE子句建立此种关联实现查询

使用子查询作派生的表

使用子查询作表达式

SELECT (SELECT AVG(chinese) FROM tb_demo071) AS yuwen ,(SELECT AVG(english) FROM tb_demo071) AS yingyu,(SELECT AVG(math) FROM tb_demo071) AS shuxue FROM tb_demo071

算术表达式、聚合函数

AVG 平均数 count求和

reference

https://juejin.cn/post/6918624554201907207 in exists