3

I am trying to copy data from table to another mysql table from two different databases
First I get all data from first table and stored them into php variable
then I loop on that var to insert data to the other table
using this query

INSERT INTO `users` (`nUserId`, `vLoginName`, `vFirstName`, `vLastName`, `vEmail`)
    VALUES ('$entity_id','$name','$firstname','$lastname','$email') 
    WHERE NOT EXISTS (SELECT `nUserId`,`vEmail`
                      FROM `users`
                      WHERE `nUserId`='$entity_id' 
                      AND   `vEmail` = '$email')

but it's not working

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'WHERE NOT EXISTS (SELECT `nUserId`,`vEmail` FROM users ' 

What the correct syntax to make this work ?

Ashraf Hefny
  • 133
  • 1
  • 1
  • 5

5 Answers5

2

I think the problem might be VALUES and I would write the statement as the following:

INSERT INTO `users` 
    (`nUserId`, `vLoginName`, `vFirstName`, `vLastName`, `vEmail`)
SELECT 
    '$entity_id', '$name', '$firstname', '$lastname', '$email' 
FROM dual
WHERE NOT EXISTS (SELECT *
                    FROM `users`
                    WHERE `nUserId`='$entity_id' 
                    AND   `vEmail` = '$email')
ypercubeᵀᴹ
  • 97,895
  • 13
  • 214
  • 305
SQLHound
  • 261
  • 2
  • 14
2

you can try something like

delimiter $$
create procedure select_or_insert()
begin
  IF EXISTS (select * from users where username = 'something') THEN
    update users set id= 'some' where username = 'something';
 ELSE 
insert into users (username) values ('something');
  END IF;
end $$
delimiter ;
Bill N. Varelli
  • 687
  • 1
  • 6
  • 18
1

You cannot use WHERE clause in an INSERT statement. However, you may use INSERT IGNORE if in your targer table you define nUserId or vEmail as unique keys.

More info about INSERT syntax: https://dev.mysql.com/doc/refman/5.6/en/insert.html

Jehad Keriaki
  • 3,081
  • 1
  • 15
  • 15
1

No need to store them in php.

Creates a table excactly like the one specified, Note: copies only columns

CREATE TABLE tbl_name LIKE db_name.tbl_name;

If you want specific column names:

CREATE TABLE tbl_name SELECT column_name, column_name FROM db_name.tbl_name;

Then if you want to copy all the data do

INSERT INTO tbl_name SELECT * FROM db_name.tbl_name;

Or specific column data do

INSERT INTO tbl_name SELECT column_name, column_name FROM db_name.tbl_name;

If you want specific data add a WHERE clause.

0
$existeEmail=$mysqli->query("SELECT * FROM tb_users WHERE email = '$email'");
 if(!$existeEmail=mysqli_fetch_array($existeEmail))/*si existe en la tabla tblreseteopass*/
{



}else{
    $email = $existeEmail['email'];
      // Direccionado hacia el registro con la Alerta 001
            // ya existe una cuenta asociada a este email, si no recuerda la contraseña, solicite una nueva
            header("Location: https://www.yourpage.com/b/?m=f_lg);
}
Glorfindel
  • 2,201
  • 5
  • 17
  • 26
  • Hola Jorge. Acqui, we normally write in English! If you can't do that, then use Google translate or similar to get your Spanish into English. p.s. Benvenido en el forum! :-) – Vérace Apr 16 '19 at 06:24