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 8194045
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T04:41:18+00:00 2026-06-07T04:41:18+00:00

I know I should show what I’ve tried so that I can get better

  • 0

I know I should show what I’ve tried so that I can get better help, but I don’t even know where to start on this one, so there’s nothing to show. I was doing well to formulate this into an intelligent question. I am using PHP and MySQL:

arpr_customs (`id`, `customfield_id`, `contact_id`, `stamp_create`, `stamp_update`, `field_value`)
arpr_contacts (`id`, `stamp_create`, `stamp_update`, `key`, `email_address`, `title`, `first_name`, `middle_name`, `last_name`, `full_name`, `format_preference`, `company`, `department`, `address_1`, `address_2`, `address_3`, `city`, `state`, `postal_code`, `country`, `alternative_email_address_1`, `alternative_email_address_2`, `alternative_email_address_3`, `phone_number_1`, `phone_number_2`, `phone_number_3`, `mobile_phone_number_1`, `mobile_phone_number_2`, `mobile_phone_number_3`, `fax_number_1`, `fax_number_2`, `fax_number_3`, `referer_id`, `schedule_pid`, `export_pid`, `import_subscribe_ip_address`, `import_subscribe_date_time`, `import_confirm_ip_address`, `import_confirm_date_time`, `import_customs`, `import_confirmed`)

Given these two tables, I need to identify contact_ids where there is no row in arpr_customs that contains that contact_id and customfield_id ‘3’.
once identified, I need to insert into arpr_customs a new row for each of them where the “id” is allowed to auto-increment, The customfield_id is set to “3”, the “contact_id” is set to the the found id. and field_value is set to “Susie”

(Susie represents the name of a sales person who will take ownership of all currently un-owned accounts.)
(customfield_id “3” is Salesperson name.)

Edit:
I’d have put this in a comment, but it’s too big.
I took spencer’s advice and worked on a duplicate table. Here’s what actually worked for me. Thanks @spencer

INSERT INTO testing_customs (`contact_id`,`stamp_create`,`stamp_update`,`customfield_id`,`field_value`) 
SELECT o.id AS contact_id, 1341602090 as stamp_create, 1341602090 as stamp_update, 3 AS customfield_id,'Susie' AS field_value
FROM arpr_contacts o LEFT JOIN testing_customs u ON u.contact_id = o.id
AND u.customfield_id = 3  WHERE u.id IS NULL 
  • 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-07T04:41:20+00:00Added an answer on June 7, 2026 at 4:41 am

    To find rows in arpr_contacts which have no “matching” rows in the arpr_customs table (assuming here that arpr_customs.contact_id is a foreign key that points to arpr_contacts.id

    SELECT o.id
      FROM arpr_contacts o
      LEFT 
      JOIN arpr_customs u
        ON u.contact_id = o.id
           AND u.customfield_id = 3
     WHERE u.id IS NULL
    

    That’s a familiar “anti-join” pattern. Basically, the statement is saying get all rows from contacts, along with all matching rows from customs… and then throw out any rows that matched, so we are left with rows from contacts for which there was no match. (We’re depending on having a column with a NOT NULL constraint in the customs table, so we can test whether we had a match or not. I’m assuming here that the id column is the primary key, which we know can never be null. (NOTE: the actual execution plan for this query is a little different than what I described, but conceptually, that’s the end result of what’s happening.)

    As a (usually) slower alternative, you can also use a correlated subquery to get the same result:

    SELECT o.id
      FROM arpr_contacts o
     WHERE NOT EXISTS (SELECT 1 FROM arpr_customs u 
                        WHERE u.contact_id = o.id 
                          AND u.customfield_id = 3)
    

    Or, if you’re going for horrible performance on really large tables, you can use a NOT IN predicate. (Just be careful here to avoid getting any NULL values in the list.)

    SELECT o.id
      FROM arpr_contacts o
     WHERE o.id NOT IN 
           ( SELECT u.contact_id 
               FROM arpr_customs u
               WHERE u.contact_id IS NOT NULL
                 AND u.customfield_id = 3
           )
    

    That only partially answers your question.

    I notice now, as I was reviewing, that I left out any predicate on that customfield_id column. FIXED

    And it doesn’t address inserting the “missing” rows…

    You can grab whatever columns you need of the arpr_contacts table, and then supply values for the other columns, to do an INSERT … SELECT …

    I’d recommend you run just the SELECT part, and then (if you aren’t exactly sure), create a temporary table as a standin to accept the insert. (You can grab the output from a SHOW CREATE TABLE arpr_customs, and edit that (remove foreign key constraints, and change the table name, and use that as a target for your insert.)

    INSERT INTO arpr_customs (`contact_id`,`customfield_id`,`field_value`)
    SELECT o.id    AS contact_id
         , 3       AS customfield_id
         , 'Susie' AS field_value
      FROM arpr_contacts o
      LEFT
      JOIN arpr_customs u
        ON u.contact_id = o.id
           AND u.customfield_id = 3
     WHERE u.id IS NULL
    

    If the id column in the arpr_customs table is defined as AUTO_INCREMENT, a value will automatically be assigned for it.

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

Sidebar

Related Questions

I know that the following code should show and hide a tiny circular progress
I know I should write this in Vb.net but for my own reasons I'm
I know I should be able to solve this myself, but I've been banging
I know this should be a basic question but I am hitting a brick
I know I should do my homework on my own but I simply can't
I know W3C Validator can do this but i need any free offline editor.
This question may look to be very simple but i don't know the answer.
I don't know if it is a common problem, but this strange problem is
I know you should use a StringBuilder when concatenating strings but I was just
I am trying to find a good example that can help me to understand

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.