SQL Syntax

This select query is correct. I need to update j.LAST_WORK in the job table with the results from the query.

SELECT j.JOB, MAX(l.DATE_WORK) 'LAST DAY WORKED'
FROM job j, labor l
WHERE j.JOB_ID = l.JOB_ID
AND j.JSTATUS = 'I'
AND j.DELETED = 'N'
AND l.DELETED = 'N'
GROUP BY j.JOB_ID;
hdcowboyazAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

PortletPaulfreelancerCommented:
Please try this - BUT, you don't specify what field in table JOB that you are updating so I have assumed a field name of `LAST_DAY_WORKED`, similarly I have assume JOB_ID is the field to join by:
UPDATE JOB AS target
INNER JOIN (
            SELECT j.JOB_ID, MAX(l.DATE_WORK) AS LAST_DAY_WORKED
            FROM job j 
            INNER JOIN labor l ON j.JOB_ID = l.JOB_ID
            WHERE j.JSTATUS = 'I'
            AND j.DELETED = 'N' 
            AND l.DELETED = 'N'
            GROUP BY j.JOB_ID
           ) AS source
             ON target.JOB_ID = source.JOB_ID
SET target.LAST_DAY_WORKED = source.LAST_DAY_WORKED
;

Open in new window

note I have used ANSI join syntax - and suggest you adopt this as standard practice.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
hdcowboyazAuthor Commented:
Couple typos but it worked...thanks

UPDATE job AS target
INNER JOIN (
            SELECT j.JOB_ID, MAX(l.DATE_WORK) AS LAST_DAY_WORKED
            FROM job j
            INNER JOIN labor l ON j.JOB_ID = l.JOB_ID
            WHERE j.JSTATUS = 'I'
            AND j.DELETED = 'N'
            AND l.DELETED = 'N'
            GROUP BY j.JOB_ID
           ) AS source
             ON target.JOB_ID = source.JOB_ID
SET target.LAST_WORK = source.LAST_DAY_WORKED;
0
PortletPaulfreelancerCommented:
"couple of typos"?
do you mean the lower case job in line 1?

as you didn't identify the field to update I did say I had made a guess, so the difference in the last line isn't a typo - is was an assumed field name.

I cannot see any other differences. Are there any others?

Anyway, you got the idea and that is what we aim at. Thanks for the quick closure that's always appreciated.

Cheers, Paul
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
MySQL Server

From novice to tech pro — start learning today.