English 中文(简体)
如果存在数据,如何进行更新?
原标题:How to perform either update query if data is present else insert the data in Mysql?

我想知道如果数据存在,那么就必须有我的ql更新问题,否则将数据列入我的ql表格

What I have done is, I am fetching data from .csv file into mysql using fgetcsv() function. Then reading each row from csv & putting those four values in four variables & then firing update query. SO how can I check that if one of the data is not present still update query is working(0 rows successfully updated) so I want to identify that data & insert that into table else if data present my update query will work.

I want to either update or insert data into product_entity_price table. My script-:

    if(($handle = fopen("/data/sample/items_0420.csv", "r")) !== FALSE) 
    {          
         while (($data = fgetcsv($handle, 8000, ",")) !== FALSE)      
         {
        $num = count($data);
        for ($c=0; $c < $num; $c++) 
        {             
            $temp = $data;             
            $string = implode(";", $temp);           
        }
            $pieces = explode(";", $string);               
            $col1 = $pieces[0];
            $col2 = $pieces[1];         
            $col3 = $pieces[2];               
            $col4 = $pieces[3]; 

    $query_insert = "INSERT INTO import_prices (customer_id, sku, price, website) ".             
    "VALUES($col1, $col2 ,$col3, $col4 ) ".             
    "ON DUPLICATE KEY UPDATE customer_id=$col1, price=$col3,website= $col4  "; 
        $result = mysql_query($query_insert);

             $query_update = "UPDATE  product_entity_price pep 
             JOIN   catalog_product cp   ON    cp.entity_id = pep.entity_id    
             AND    cp.sku LIKE  $col2    AND   website_id = $col4 
             JOIN   customer_group cg   ON    cg.customer_group_id = pep.customer_group_id   AND   cg.customer_group_code =  $col1 
SET cpetp.value = ".$col3.";";
             mysql_query($query_update);
          }
    }
最佳回答
问题回答

我总是发现,使用两个问题最多的是reliable:

$result = mysql_query("SELECT 1 FROM import_prices WHERE ...");
list($exists) = mysql_fetch_row($result);

if ($exists)
    $result = mysql_query("UPDATE import_prices ...");
else
    $result = mysql_query("INSERT INTO import_prices ...");

这种做法的倒数是,你总是提出两个问题而不是一个问题。 但是,这种做法对没有主要关键内容的表格(如关系表)有用。

你们还可以尝试做更新的问询,然后检查<条码>mysql_ Affect_rows,并在0时插入。 如果你做更新,所有旧的数值与新的数值相同,mysql_ Affect_row<>/code>仍将返回。 页: 1





相关问题
SQL SubQuery getting particular column

I noticed that there were some threads with similar questions, and I did look through them but did not really get a convincing answer. Here s my question: The subquery below returns a Table with 3 ...

please can anyone check this while loop and if condition

<?php $con=mysql_connect("localhost","mts","mts"); if(!con) { die( unable to connect . mysql_error()); } mysql_select_db("mts",$con); /* date_default_timezone_set ("Asia/Calcutta"); $date = ...

php return a specific row from query

Is it possible in php to return a specific row of data from a mysql query? None of the fetch statements that I ve found return a 2 dimensional array to access specific rows. I want to be able to ...

Character Encodings in PHP and MySQL

Our website was developed with a meta tag set to... <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> This works fine for M-dashes and special quotes, etc. However, I ...

Pagination Strategies for Complex (slow) Datasets

What are some of the strategies being used for pagination of data sets that involve complex queries? count(*) takes ~1.5 sec so we don t want to hit the DB for every page view. Currently there are ~...

Averaging a total in mySQL

My table looks like person_id | car_id | miles ------------------------------ 1 | 1 | 100 1 | 2 | 200 2 | 3 | 1000 2 | 4 | 500 I need to ...