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

  • Home
  • SEARCH
  • 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 8209567
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T09:46:06+00:00 2026-06-07T09:46:06+00:00

What im trying ot do is update the first instance of a record based

  • 0

What im trying ot do is update the first instance of a record based on contact_id.

I have a table of email addresses and corresponding contact_id’s. Each contact may have more than one email address so the table can contain more than one instance of the same contact_id.

I want to update the corresponding is_primary field to ‘1’, when I hit the first instance of each distinct contact_id.

I’m not too concerned about what ends up being the primary email address as long as everyone in the database has one.

Table Example:

 contact_id   | email        |      is_primary
---------------------------------------------
 1001         | john@world.com      |0
 1001         | desmond@world.com   |0
 1002         | person@life.com     |0
 1003         | preson@help.com     |0
 1003         | bad@sql.com         |0
 1003         | have@searched.com   |0

So the result im looking for would be:

 contact_id   | email        |      is_primary
---------------------------------------------
 1001         | john@world.com      |1
 1001         | desmond@world.com   |0
 1002         | person@life.com     |1
 1003         | preson@help.com     |1
 1003         | bad@sql.com         |0
 1003         | have@searched.com   |0

Have tried creating a temp table and macthcing to that, but query updated all is_primary fields. I know im missing something basic here, but my sql skills are limited.

  • 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-07T09:46:08+00:00Added an answer on June 7, 2026 at 9:46 am

    This technique joins the table against itself in a subquery, but it only matches one row (based on contact_id and email matching. The trick is that the subquery returns just one of the email addresses using MIN, theoretically the first one alphabetically (not reliable, but you said that didn’t matter).

    I have tested this with good results.

    UPDATE 
      email 
      JOIN (SELECT contact_id, MIN(email) as email 
            FROM email GROUP BY contact_id) as singles 
      USING(contact_id, email) 
    set is_primary=1;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to update a table to remove all but the first instance of
I am trying to use an update query to update the first 3 characters
So I am trying to update NodeJS and running into issues. First I tried
I am trying to get my first button to update a display number in
Im trying to update a table with the following code. If I change WHERE
I am trying update my version of sqlite3 on mac os x 10.5.7 I
I am trying update text views while this loop is processing. Here is my
I trying to update a model on a callback but the validation is causing
When trying to update a subversion working copy from Netbeans, I get the following
Im trying to update the contents of an element after running some php code.

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.