Dexter
Dexter

Reputation: 982

INSERT in table1 based on condition on table2 - MySQL

I have two tables t1 and t2 in MySQL. I've to insert records into t1 when the following conditions on t2 are TRUE: version != -1 AND login = "xyz".

I tried this query but no luck:

INSERT INTO t1(name, login, email, add)
SELECT "john", "john159", "[email protected]", "dallas"
WHERE EXISTS (
SELECT version, login 
FROM t2
WHERE login = "john159" 
AND version != -1);

Upvotes: 0

Views: 37

Answers (1)

Jacob
Jacob

Reputation: 2444

Please try the following, I haven't executed the same on the editor but I hope you will get a different idea with it.

INSERT INTO T1(name, login, email) 
        SELECT "john", "john159", "[email protected]"
            FROM dual
            WHERE EXISTS (SELECT * FROM T2
                                 WHERE login = "john159" 
                                   AND version  <> -1)

Upvotes: 1

Related Questions