gpt4 book ai didi

mysql更新具有多个表和左外连接的select语句

转载 作者:行者123 更新时间:2023-11-30 00:35:53 25 4
gpt4 key购买 nike

我有两个表,hires_owner和projects,其中hirees_owner是项目中数据的汇总表。我想使用项目中的数据定期更新hires_owner。表结构如下:

**hires_owner**

id INT(11) AUTO-INCREMENT
owner CHAR(25) UNIQUE
hires_total INT(3)

Sample data:
1, tim, 0
2, jack, 3
3, brian, 1
etc.

**projects**

id INT(11) AUTO-INCREMENT
date DATE() **this is the report date stamp, not date of activity
owner CHAR(25)
accept DATE()

sample data:
1, 2014-02-01, jack, 2014-01-02
2, 2014-02-01, jack, 2014-01-03
3, 2014-02-01, tim, NULL
etc.

此查询获取我想要推送到hires_owner表中的结果:

select owner, count(accept) 
from projects
where date = (select max(date) from projects)
group by owner

...但我似乎无法正确获取更新查询。这是一次尝试:

update hires_owner h
set hires_total = p.Hires
(select owner, count(accept) as Hires
from projects
where date = (select max(date) from projects)
group by owner) p
where p.owner = h.owner

最佳答案

试试这个:-

update
hires_owner h
inner join
(select owner, count(accept) num_c
from projects
where date = (select max(date) from projects)
group by owner) p
on h.owner = p.owner
set h.hires_total = num_c

关于mysql更新具有多个表和左外连接的select语句,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22182646/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com