1

I have a table of records, which has a self-relationship.

Additionally - to make searching easier - I have a flag which determines that a record has been referenced and hence that row is now "obsolete" and is only there for audit purposes:

CREATE TABLE Records
(
  RecordID INT(5) NOT NULL,
  Replaces INT(5) NULL,
  Obsolete INT(1) NOT NULL
)

RecordID is the PK, Replaces links to a previous RecordID which has now been replaced, and Obsolete is redundant information which just says that another record has replaced this one. It just makes searching a lot easier. The table is very large. These are just 3 of the columns.

The only problem is: there was a typo in one of the queries in the system so for a small set of rows, the Obsolete value was not set to 1 (true).

This query will show all the records with Obsolete equal to 0 which should be equal to 1:

   SELECT *
     FROM Records AS rec1
LEFT JOIN Records AS rec2
       ON rec1.Replaces = rec2.RecordID
    WHERE rec2.RecordID IS NOT NULL
      AND rec2.Obsolete = 0;

Now I need to run an UPDATE to change all those req2.Obsolete from 0 to 1, but I'm not sure how to write a query with an INNER JOIN.

3 Answers 3

1

You don't need an inner join. Since your query already returns the records that need to be updated, just do this:

Update Records
set Obsolete=1 where
RecordID in (
 SELECT rec2.RecordID     
        FROM Records AS rec1
LEFT JOIN Records AS rec2
       ON rec1.Replaces = rec2.RecordID
    WHERE rec2.RecordID IS NOT NULL
      AND rec2.Obsolete = 0
)
1
  • "You don't need an inner join" -- if your LEFT OUTER JOIN is semantically equivalent to an INNER JOIN then doesn't that make it an inner join?
    – onedaywhen
    Oct 25, 2011 at 7:19
1
UPDATE Records
SET obsolete = 1
WHERE recordID in (
SELECT rec1.recordid
     FROM Records AS rec1
LEFT JOIN Records AS rec2
       ON rec1.Replaces = rec2.RecordID
    WHERE rec2.RecordID IS NOT NULL
      AND rec2.Obsolete = 0
)
0

I would suggest doing this in two steps using a temporary table:

-- Create temporary table for holding RecordIDs to be marked as obsolete
CREATE TEMPORARY TABLE `mark_obsolete` (`RecordID` INT NOT NULL);

-- Insert RecordIDs to mark as obsolete into temp table
INSERT INTO `mark_obsolete` (`RecordID`)
SELECT `rec2`.`RecordID`
FROM
    `Records` AS `rec1`
    INNER JOIN `Records` AS `rec2`
        ON `rec1`.`Replaces` = `rec2`.`RecordID`
WHERE `rec2`.`Obsolete` = 0;

-- Update records using inner join to temp table
UPDATE
    `Records` AS `r`
    INNER JOIN `mark_obsolete` AS `o`
        ON `r`.`RecordID` = `o`.`RecordID`
SET `r`.`Obsolete` = 1;

DROP TEMPORARY TABLE `mark_obsolete`;

Note that using a LEFT JOIN with WHERE rec2.RecordID IS NOT NULL is the same as an INNER JOIN.

The reason for using a temporary table is to avoid locking issues when updating the same table used in the sub-query. And it might also give you better performance than using the IN clause.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.