MySql 怎么实现级联查询

MySql 怎么实现级联查询,第1张

首先能实现目的的语法是这么写:

select id,name,a_rankfrom table_a,(select a_id,count(*) as a_rankfrom table_bgroup by a_id)where table_a.id = table_b.a_idorder by a_rank

mysql:

1、先给B表添加主键

alter table tableB add constraint b_PK primary key(date,channel)

2、将B表的组合主键关联到A表的外键组合键中

alter table tableB add constraint tableB_fk foreign key(date,channel) references tableA (date,channel) on DELETE CASCADE

3 delete from tableA where date =? and channel =? 就出发相关记录··~

create trigger update_id on a

for delete

as

Declare @qqlx_bid varchar(20)

Declare @qqlx_cid varchar(20)

--获得删除记录

select @qqlx_bid=bid,@qqlx_cid=cid from deleted

update b set bid=0 where bid=@qqlx_bid

update c set cid=0 where cid=@qqlx_cid

end

go


欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/zaji/8520308.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-04-17
下一篇 2023-04-17

发表评论

登录后才能评论

评论列表(0条)

保存