I have already asked this but question was closed without answer so I post new question.
I am trying to add values that are missing in second table
in table ps_psroduct supplier some values are missing and I want to add the from ps_product. So I want to list values that are missing and insert them in ps_product_supplier, there is no duplication.
Some values from ps_product are same in ps_product_supplier but not all so I want to filter thoese values and insert them correctly.
So some values exists in ps_product but not in ps_product_supplier and I want to insert missing rows in table by checking id_product and add missing.
What I have for now is that I get all the values printed but I need only missing values that I can insert them in ps_product_supplier
How to do that?
In output I get:
USPJESNO ID 1 ne postoji u drugoj tablici i trenutno: 0
USPJESNO ID 2 ne postoji u drugoj tablici i trenutno: 1
USPJESNO ID 3 ne postoji u drugoj tablici i trenutno: 2
USPJESNO ID 4 ne postoji u drugoj tablici i trenutno: 3
USPJESNO ID 5 ne postoji u drugoj tablici i trenutno: 4
USPJESNO ID 6 ne postoji u drugoj tablici i trenutno: 5
for all the values, but I need to mach IDs and print only missing and create a row for it in ps_product_supplier with values from ps_product
$kveri = "SELECT id_product,id_supplier,supplier_reference, wholesale_price FROM ps_product";
$ispis = mysql_query($kveri) or die(mysql_error());
while ($row = mysql_fetch_array($ispis)){
$trenutnired = $row['id_product'];
$trenutnired1 = 0;
$trenutnired2 = $row['id_supplier'];
$trenutnired4 = $row['supplier_reference'];
$trenutnired5 = $row['wholesale_price'];
$trenutnired6 = 3;
//echo $trenutnired;
//$drugatab = "SELECT * FROM ps_product_supplier WHERE id_product = '$trenutnired'";
$drugatab = "SELECT id_product FROM ps_product_supplier WHERE id_product = '$trenutnired'";
$rezultati = mysql_query($drugatab) or die(mysql_error());
if(mysql_num_rows($rezultati)){
// Successful query...
echo "USPJESNO ID $trenutnired ne postoji u drugoj tablici i trenutno: $countUpdated<br />";
//mysql_query("INSERT INTO ps_product_supplier (id_product_supplier, id_product, id_product_attribute, id_supplier, product_supplier_reference, product_supplier_price_te, id_currency) VALUES ('', '$trenutnired', '$trenutnired1', '$trenutnired2', '$trenutnired4', '$trenutnired5', '$trenutnired6')");
$countUpdated++;
} else {
//$countUpdated++;
echo "ID $trenutnired ne postoji u drugoj tablici i trenutno: $countUpdated<br />";
}
}
This query works for one ID to insert data but I need for all that are missing:
INSERT INTO ps_product_supplier
(id_product_supplier, id_product, id_product_attribute, id_supplier,
product_supplier_reference, product_supplier_price_te, id_currency)
VALUES(NULL, 6216, 0, (SELECT id_supplier FROM ps_product WHERE id_product = 6216),
(SELECT supplier_reference FROM ps_product WHERE id_product = 6216),
(SELECT wholesale_price FROM ps_product WHERE id_product = 6216), 3);
I think you’re trying to select products that are in ps_products, but are not in ps_productsuppliers. If I’m right about that, this way is probably the easiest to understand.