Question

I have two tables, hires_owner and projects, where hires_owner is a summary table of data in projects. I'd like to update hires_owner periodically with data from projects. Table structure is as follows:

**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.

and

**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.

This query gets me the results that I want to push into the hires_owner table:

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

... but I can't seem to get the update query correct. Here's one attempt:

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
Était-ce utile?

La solution

Try this:-

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

Autres conseils

Your query does not specify which owner to update and it is a little bit twisting logic. Updated,Let me know the result.

update hires_owner set hires_total = p.m
from (select max(count(accept)) as m, owner as o
                   from projects 
                   where date = (select max(date) from projects)
                   group by owner) as p
where hires_owner = p.o;
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top