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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T19:02:35+00:00 2026-06-09T19:02:35+00:00

I am new to mysql. I have a problem in inserting record to table1

  • 0

I am new to mysql. I have a problem in inserting record to table1 if it does not exist in table2.I have 2 tables table1 and table2 in the form:

table1

dep_id  start     stop     modified deleted                 
1       23456789  167921525   Yes      No
2       34567812  345678145   Yes      No
3       32789054  327890546   No       No


table2

start     stop     modified deleted                 
23456789  167921525  No       No
34567823  345678145  No       No
32789053  727890546  No       No

I am trying to insert values into table1’s start and stop field values only if it does not exist in table2’s “start” and “stop” columns. If it exists the I need to throw an error.
These tables do not have a primary key foreign key relationship.
I apologize for not knowing correct syntax but I have to do something like this in mysql and PHP.

Replace Into into table1 set 'start'=> $start,'stop' => $stop
(select 'start','stop' from table2 where table1.start and table1.stop not in table2.start and table2.stop);

How do I query these 2 tables to check if Table1.start and Table1.stop fields do not match with Table2.start and Table2.stop before inserting to table1?

  • 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-09T19:02:37+00:00Added an answer on June 9, 2026 at 7:02 pm

    You can do:

    INSERT INTO table1 (start, stop)
    SELECT    a.*
    FROM      (SELECT 123456789 start, 234567890 stop) a
    LEFT JOIN table2 b ON (a.start,a.stop) IN ((b.start,b.stop))
    WHERE     b.start IS NULL
    

    Where 123456789 and 234567890 are your input values for start and stop respectively.

    Then you can check it with rowCount or num_rows_affected based on what DB interface you’re using (PDO, mysqli, etc.). If it’s 0, then no record was inserted, otherwise, the insert occurred.


    SQLFiddle Demo

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

Sidebar

Related Questions

So I have this JS form that creates a new record ( MySQL ,
Table in MySQL have 1 problem column: creation_date . During inserting a new row
I'm quite new to MySQL and I'm confused with one problem: I have one
I'm new to MySQL and PHP. I have two tables, one to hold all
I am relatively new to PHP/MySQL and have been having a problem that I
I am new to MYSQL and have a problem. Its related to php too
I'm fairly new to using MSSQL and have run into a weird problem. Given
I'm very new to MySQL and I have a stored procedure that I'd like
I am new to MySQL, but I have used Oracle for some time. I
I am new to MySQL coming from Oracle. I have a requirement to create

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.