Solved

Update with a Join?

Posted on 2011-03-03
5
360 Views
Last Modified: 2012-05-11
Hello everyone,

I have 2 tables.  Lets call them audit and docs.

audit contains a list of docid's with a time/date stamp
docs contains the same list of docid's and also contains a flag field.

I want to update docs.flag to 0 where audit.timestamp is equal to a date.

Both tables contain docid.

I assume this is a join, I'm just not sure how to do it, can anybody assist please?

Kind regards,

Darren Bridle
0
Comment
Question by:dbridle
  • 2
  • 2
5 Comments
 
LVL 3

Expert Comment

by:mwiercin
Comment Utility
Hi!

This will do:

UPDATE 
  docs d,
  JOIN audit a USING (docid) 
SET 
  d.flag = 0
WHERE
   a.timestamp = '2011-11-11 11:11:11'

Open in new window


Btw. It's always a good idea to rewrite that query to be a SELECT before you run it, just to make sure it will update what you actually want.
0
 
LVL 38

Accepted Solution

by:
Aaron Tomosky earned 300 total points
Comment Utility
Update docs set flag  = 0 where docid in (select docid from audit where timestamp = 'date')
0
 

Author Comment

by:dbridle
Comment Utility
Assuming I use the update join, this is the query:

UPDATE
    p2.neocol_document_extract_list d,
    JOIN p2.neocol_audit a USING (f_docnumber)
SET
    d.import_flag = 0
WHERE
    a.importdate > '2011-02-08 00:00:00'
and
    a.importdate < '2011-02-08 23:59:00';

There is an error at the JOIN part of the query:

Error Code: 1064
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'JOIN p2.neocol_audit a USING (f_docnumber)
WHERE
    a.importdate > '2011-02-08' at line 3
0
 
LVL 3

Assisted Solution

by:mwiercin
mwiercin earned 200 total points
Comment Utility
It's because of the comma after p2.neocol_document_extract_list d, my example was flawed :)

Try

UPDATE 
    p2.neocol_document_extract_list 
    JOIN p2.neocol_audit a USING (f_docnumber)
SET 
    d.import_flag = 0
WHERE
    a.importdate > '2011-02-08 00:00:00' 
and 
    a.importdate < '2011-02-08 23:59:00';

Open in new window

0
 

Author Comment

by:dbridle
Comment Utility
I went ahead and used Update docs set flag  = 0 where docid in (select docid from audit where timestamp = 'date') and it worked a treat.

Thank you both for your help, I know 2 ways to do that now!
0

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Composite queries are used to retrieve the results from joining multiple queries after applying any filters. UNION, INTERSECT, MINUS, and UNION ALL are some of the operators used to get certain desired results.​
Using SQL Scripts we can save all the SQL queries as files that we use very frequently on our database later point of time. This is one of the feature present under SQL Workshop in Oracle Application Express.
Video by: Steve
Using examples as well as descriptions, step through each of the common simple join types, explaining differences in syntax, differences in expected outputs and showing how the queries run along with the actual outputs based upon a simple set of dem…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now