Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Update with a Join?

Posted on 2011-03-03
5
Medium Priority
?
374 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
[X]
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
  • 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 1200 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 800 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

Use Filtering Commands to Process Files in Linux

Learn how to manipulate data with the help of various filtering commands such as `cat`, `fmt`, `pr`, and others in Linux.

Question has a verified solution.

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

This article shows how to get a list of available printers for display in a drop-down list, and then to use the selected printer to print an Access report or a Word document filled with Access data, using different syntax as needed for working with …
Instead of error trapping or hard-coding for non-updateable fields when using QODBC, let VBA automatically disable them when forms open. This way, users can view but not change the data. Part 1 explained how to use schema tables to do this. Part 2 h…
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…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…

688 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