-1

我已经问过这个问题了,但问题没有回答就关闭了,所以我发布了新问题。我正在尝试在表 ps_psroduct 供应商的第二个表中添加缺少的值,一些值丢失了,我想添加来自 ps_product 的值。所以我想列出缺少的值并将它们插入到 ps_product_supplier 中,没有重复。ps_product 的一些值在 ps_product_supplier 中是相同的,但不是全部,所以我想过滤这些值并正确插入它们。所以某些值存在于 ps_product 但不存在于 ps_product_supplier 中,我想通过检查 id_product 并添加缺失来在表中插入缺失的行。我现在所拥有的是我打印了所有值,但我只需要缺失值,我可以将它们插入到 ps_product_supplier

怎么做?在输出中我得到:

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

对于所有值,但我需要处理 ID 并仅打印缺失的内容并在 ps_product_supplier 中为它创建一行,其中的值来自 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 />";
        }
        }

此查询适用于一个 ID 以插入数据,但我需要所有缺少的内容:

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);
4

1 回答 1

1

我认为您正在尝试选择ps_products 中但不在ps_productsuppliers 中的产品。如果我是对的,那么这种方式可能是最容易理解的。

select id_product
from ps_products
where id_product not in (select id_product from ps_product_supplier;
于 2012-12-27T19:20:49.637 回答