0

I have been comparing 2 tables using JOIN and storing my result in another table called macadd. Each time I run my code its generating duplicate record in table macadd. How I can stop same record being stored again and again. Here is code which is generating duplicates:

$query = mysqli_query($conn, "insert into macadd (mac)
select volley.mac from volley
inner join savedemail on savedemail.email = volley.email");

As This query is comparing the rows. I am completely out of logic here that how can I make a variable out of this sql query. So that using that I can check weather record exist or not

  • 1
    Do you want insert if not exists? http://stackoverflow.com/questions/3164505/mysql-insert-record-if-not-exists-in-table – Kelvin Feb 18 '16 at 15:21
  • Yes! ofcourse i want to insert if not exists –  Feb 18 '16 at 15:22
  • i am not able to use `(mysqli_num_rows($query) > 0)` here because there is no variable through which I can validate it, –  Feb 18 '16 at 15:24
  • you need slice your query to 2 other query. The first you check exists, and if not exist you run the second query : `insert` – Kelvin Feb 18 '16 at 15:28

1 Answers1

0

You want to use INSERT IGNORE.

$query = mysqli_query($conn, "insert ignore into macadd (mac)
select volley.mac from volley
inner join savedemail on savedemail.email = volley.email");
Jim Wright
  • 5,256
  • 1
  • 9
  • 30