Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Update with a Join?

Posted on 2011-03-03
5
369 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
ID: 35032578
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 39

Accepted Solution

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

Author Comment

by:dbridle
ID: 35032649
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
ID: 35032660
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
ID: 35032675
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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Entering a date in Microsoft Access can be tricky. A typo can cause month and day to be shuffled, entering the day only causes an error, as does entering, say, day 31 in June. This article shows how an inputmask supported by code can help the user a…
Read about achieving the basic levels of HRIS security in the workplace.
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…

839 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