4

I have two tables that store value as VARCHAR.
I'm populating table and I want just insert values in one of tables if they are not exist in other table.
Something like:

INSERT IF IS EMPTY(SELECT * FROM t1 where v='test') INTO t2 (v) VALUES ('test')

How Can I do that?

5 Answers 5

7

You need to use some type of INSERT...SELECT query.

Update (after clarification): For example, here is how to insert a row in t2 if a corresponding row do not already exist in t1:

INSERT INTO t2 (v)
  SELECT temp.candidate
  FROM (SELECT 'test' AS candidate) temp
  LEFT JOIN t1 ON t1.v = temp.candidate
  WHERE t1.v IS NULL

To insert multiple rows with the same query, I 'm afraid there is nothing better than

INSERT INTO t2 (v)
  SELECT temp.candidate
  FROM (
      SELECT 'test1' AS candidate
      UNION SELECT 'test2'
      UNION SELECT 'test3' -- etc
  ) temp
  LEFT JOIN t1 ON t1.v = temp.candidate
  WHERE t1.v IS NULL

Original answer

For example, this will take other_column from all rows from table1 that satisfy the WHERE clause and insert rows into table2 with the values used as column_name. It will ignore duplicate key errors.

INSERT IGNORE INTO table2 (column_name)
  SELECT table1.other_column
  FROM table1 WHERE table1.something == 'filter';
Sign up to request clarification or add additional context in comments.

5 Comments

But this inserts into table2 and gets values from table1 ! While I want to insert if the value doesn't exists in table1! Please See my edit on question, thanks
@4r1y4n: If the value doesn't exist in table1 because you say so, and it doesn't exist in table2 because we are trying to insert it there, then where does it exist?
In SQL query itself (See my psudo query in question - 'test' is the value)! I just want insert value only in one of tables and not both (t1 XOR t2)
As of MySQL 8.0.19 you won't need a temporary table to define the data, but use VALUE instead (dev.mysql.com/doc/refman/8.0/en/values.html).
Practical note: This works fine to insert a small number of values. But when inserting a few hundret values, and checking this against a few 100k existing values, a 200x250.000 JOIN makes the query painfully slow.
4

for insertion of Multiple columns you can try this.

INSERT INTO table_1 (column_id,column_2,column_3,column_4,column_5)
 SELECT
    table_2.*
FROM
    table_2
LEFT JOIN table_1 ON table_1.column_id = table_2.column_id
WHERE
    table_1.column_id IS NULL;

Comments

1
INSERT IGNORE INTO table_1(col1,col2,col3,col4)
  SELECT table_2.*
  FROM table_2;

The above statement does the perfect job in your scenario.

  1. Line 1: States the table where you want the data to be replicated whilst ignoring any key constraints.
  2. Line 2: selects the table and columns where data should come from
  3. Line 3: states the table from where data is to be replicated from

Comments

-2
$result = mysql_query("SELECT * FROM t1 WHERE v='test'");
if(mysql_num_rows($result) == 0){
    mysql_query("INSERT INTO t2 (v) VALUES ('test')");
}

Haven't been using mysql for a while so these functions are deprecated, but this is how I should do it.

2 Comments

This is PHP and not pure MySQL! of course this works but I want to do it using an MySQL query
Okay, with my last sentence I meant it'd be better to use PDO or MySQLi. I see I didn't get your question and unfortunately don't know the answer. Sorry!
-2

Kinda ghetto.. but works. Assumes that you are inserting a static value into 'col'

INSERT INTO t1 (col)
SELECT 'test' 
  FROM t2
 WHERE (SELECT count(*) FROM t2 WHERE v='test') = 0;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.