.net 单表一对多关系配置_2.纯干货-mysql两表之间的关系

57c9458895106fa1f7f1b96c4a19b558.png

1 两张表之间的关系

  1. 多对一

~~~ mysql foreign key (dep_id) references dep(id) on delete cascade on update cascade ~~~

  1. 多对多
  2. 一对一:在外键字段中加上unique

2 单表查询

select distinct 字段1,字段2 from 表
    where 条件
    group by 分组
    having 过滤
    order by 排序
    limit 显示多少条记录

from --> where --> group by -->having -->distinct -->order by -->limit
  1. 去重:distinct
  2. 通过四则运算显示查询

~~~ python select salary12 as annu_salary from employee; # as 给salary12 重命名为annu_salary ~~~

  1. 定义显示格式

~~~ mysql select concat("姓名",name) from employee; ~~~

  1. where 约束条件
  2. 为什么where约束条件不能用聚合函数聚合函数是分组之后进行的,where的执行是在group by 之前所以不能用聚合函数作为条件
  3. 比较运算
  4. between 80 and 100
  5. in(80,90,100)
  6. like "spl%"
  7. 判断是否是null的时候必须用is
  8. group by(分组,在where后运行)
  9. 不要用unique字段作为分组的依据(没有意义)
  10. 若没有分组,整张表默认为一组
  11. 分组完成只能根据分组依据进行select和聚合函数
    ~~~ mysql select post from employee group by post; ~~~
  12. 分组之后使用聚合函数
    1. max
    2. min
    3. avg
    4. sum
    5. count
  13. group_concat
    ~~~ mysql select post,group_concat(name) from employee group_by post; #查询每个部门的人的名字 ~~~
  14. having过滤(在分组之后进行)
  15. order_by 排序 默认升序
    ~~~ mysql select * from t1 order by age asc,id desc; #按照age升序排,如果age相同按照id降序排 ~~~
  16. limit n(限制最终打印在屏幕的条数)
  17. 使用limit作分页功能
    1. ~~~ python select * from employee 0,5 #从0开始不包含0,往后取5个 ~~~

3.连表查询

3.1表的三种连接方式

select * from employee,department;#本质原理

1.内连接(两张表的共同部分,inner join...on...)

select * from employee inner join department on employee.dep_id = department.id
  1. 左外连接(在内连接的基础上,保留左表的记录)
select * from employee left join department on employee.dep_id = department.id
  1. 右外连接(在内连接的基础上,保留右表的记录)
select * from employee right join department on employee.dep_id = department.id

3.2 select 执行顺序

from-->on-->inner|left|right join -->where -->group by -->having -->select -->distinct-->limit

3.3 子查询(将一条SQL语句放到括号里当做另外一条SQL语句的查询条件)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值