Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8233165
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T18:02:38+00:00 2026-06-07T18:02:38+00:00

I created a table with the following fields: Record: Id int Primary Key, Auto

  • 0

I created a table with the following fields:

Record:
Id                 int Primary Key, Auto Increment
ForeignId          int
IsDuplicateRecord bit NULL

Then I inserted some data:

INSERT INTO Record (ForeignId)
VALUES (5), (5), (1), (2), (3)

After that, I ran the following update statement, (found at http://archive.msdn.microsoft.com/SQLExamples/Wiki/View.aspx?title=DuplicateRows ):

UPDATE Record
SET IsDuplicateRecord = 1
WHERE Id IN (
    SELECT MAX(Id)
    FROM Record
    GROUP BY ForeignId
    HAVING COUNT(*) > 1
)

So far so good, the query affected one row, and the table now looks like this:

Id ForeignId IsDuplicateRecord
0  5         NULL
1  5         1
2  1         NULL
3  2         NULL
4  3         NULL

I was happy, because for a moment I thought everything was going to be just fine. But then a suspicion as dark as the clouds outside crossed my mind:
Dreadingly, I typed

INSERT INTO Record (ForeignId)
VALUES (1), (1)

and ran the above query again, which this time yielded:

Id  ForeignId  IsDuplicateRecord
0   0          NULL
1   5          1
2   1          NULL
3   2          NULL
4   3          NULL
5   1          NULL
6   1          1

So I figured I’d head over to StackOverflow, and see who could explain to me why the IsDuplicatedRecord field in row with ID 5 wasn’t updated to 1? Are you the one?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T18:02:40+00:00Added an answer on June 7, 2026 at 6:02 pm

    Because the SQL you ran only marks the last of the duplicates as duplicates. Try this instead:

    UPDATE Record
    SET IsDuplicateRecord = 1
    WHERE Id NOT IN (
        SELECT MIN(Id)
        FROM Record
        GROUP BY ForeignId
    )
    

    This marks second and subsequent occurences of each ForeignId as duplicates as I think you require.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Consider the following table which has the fields - id (int) and date_created (datetime):
I Have the following table: CREATE TABLE `tmp_table` ( `id` int(11) NOT NULL AUTO_INCREMENT,
I need help with the following query. create table #table1 (id int not null
I have a table: CREATE TABLE `data_table` ( `data_id` INT NOT NULL AUTO_INCREMENT PRIMARY
I have the following table structure CREATE TABLE `table` ( `id` int(11) NOT NULL
I have a simple mysql table called messages with the following fields: ID(int) MESSAGE(varchar)
in the following scenario: CREATE TABLE `table1` ( `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
I have created a table with the following columns: ObservationId, FirstCreatedDate, description, ... ...
I have created a table with the following structure- $sql = CREATE TABLE followers
I have following Table with records for ActivityLog: ID, UserId, Category, Created 1 11

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.