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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T14:14:52+00:00 2026-05-27T14:14:52+00:00

I have 3 tables: dentists, groups, and groupdentlink. Many dentists link to many groups

  • 0

I have 3 tables: dentists, groups, and groupdentlink. Many dentists link to many groups through the groupdentlink table.

So I’m trying to make a query where it will insert rows into groupdentlink (linking all dentists in the state with all the groups in the state) but only if those rows don’t already exist. In a nutshell I want to add new rows without overwriting existing ones or duplicating them.

So the intent of the query is something like:

INSERT INTO groupdentlink (f_dent_id, f_group_id, f_schedule_id)
VALUES ('$_POST[id]', '$groupid', '$scheduleid')
WHERE NOT EXISTS ('$_POST[id]', '$groupid')

And I don’t have any primary keys in the groupdentlink table.

Thank you in advance!

  • 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-05-27T14:14:53+00:00Added an answer on May 27, 2026 at 2:14 pm

    If you really want to write your own (working) query..


    INSERT INTO groupdentlink (
      f_dent_id, f_group_id, f_schedule_id
    ) SELECT 
        '$_POST[id]'  f_dent_id, 
        '$groupid'    f_group_id,
        '$scheduleid' f_schedule_id
    FROM DUAL
    WHERE NOT EXISTS (
      SELECT 1
      FROM `groupdentlink`
      WHERE 
        f_dent_id = '$_POST[id]' AND f_group_id = '$groupid'
      LIMIT 1 -- tells mysql to stop searching after first match
    )
    

    … but MySQL can handle all this for you!


    You don’t need primary keys to make MySQL handle this for you, you should add a UNIQUE key constraint on the combined set of the two columns.

    Query to add the unique key dent_group_uniq_key to groupdentlink.

    ALTER TABLE groupdentlink ADD UNIQUE KEY `dent_group_uniq_key` (
      f_dent_id, f_group_id
    );
    

    Then use INSERT IGNORE on your query:

    INSERT IGNORE INTO groupdentlink (
      f_dent_id, f_group_id, f_schedule_id
    ) VALUES (
      '$_POST[id]', '$groupid', '$scheduleid'
    )
    

    INSERT IGNORE will try to insert a row to your table, if it fails due to a key constraint it will act like nothing happen.

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

Sidebar

Related Questions

I have 3 tables Links Link ID Link Name GroupID (FK into Groups) SubGroupID
I have a table groupdentlink where I want to delete all the rows that
Many databases have tables with many columns, but ScalaQuery uses tuples to represent table
I have tables linked by FK, I query on the first table using entity
In my database I have tables that define types for example Table: Publication Types
I have two tables that are joined together. A has many B Normally you
I have tables: Teachers and Students. We have a SpecialProjects Table where Teachers and
I have tables item and store (it's a store management system). item table has
I have tables Users, Widgets and Layouts. Users have many-to-many relationship with Widgets via
I have tables foo and bar: create table foo(a int, b varchar(10), primary key

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.