2012-04-10 16 views
16

मेरे पास ऐसा कुछ कोड है जो इस तरह दिखता है। तालिका में एक ऑटोइनक्रिकमेंट फ़ील्ड भी है जिसे मुझे बनाए रखना चाहिए (यह अन्य तालिकाओं में उपयोग किया जाता है)। मैं इस कोड को सरल और अनुकूलित करना चाहता हूं।MySQL: यदि मान मौजूद है तो अपडेट करें INSERT

$query ="SELECT * FROM models WHERE col1 = 'foo'"; 
$testResult = mysql_query($query) or die('Error, query failed');  

if(mysql_fetch_array($testResult) == NULL){ 
    //insert... 
    $query ="INSERT INTO models (col1, col2, col3) 
    VALUES ('foo', 'bar', 'alph')"; 
    $result = mysql_query($query) or die('Error, query failed'); 
}else{ 
    //update... 
    $query = "UPDATE models 
     SET col1='foo', col2='bar', col3='alph' 
     WHERE col1='foo' AND col2='bar'"; 
     $result = mysql_query($query) or die('Error, query failed');   
} 

संपादित करें: प्राथमिक कुंजी आईडी वह क्षेत्र है जो स्वतः बढ़ी है। मैं इसे कभी नहीं बदलना चाहता हूं। हालांकि, जब कोई अन्य क्षेत्र डुप्लिकेट होता है, तो यह तब होता है जब मैं उस रिकॉर्ड को अपडेट करना चाहता हूं।

+4

चेक आउट: [INSERT ... पर नकली चाबी अद्यतन सिंटेक्स] (http://dev.mysql.com/doc/refman/5.0/en/insert-on-duplicate.html) – Josh

+0

किया क्या आपका मतलब है 'एक ऑटोइनक्रिकमेंट फ़ील्ड' – hjpotter92

+0

यह प्रश्न सहायक हो सकता है ... [MySQL में डेटा को ऑटो वृद्धिशील प्राथमिक कुंजी रखने के लिए कैसे डेटा डालें?] (https://stackoverflow.com/questions/8753371/how-to-insert- डेटा-टू-माइस्क्ल-हो-ऑटो-वृद्धि-प्राथमिक-कुंजी) – blackandorangecat

उत्तर

29

कैसे के बारे में प्रतिस्थापन:

REPLACE INTO models 
(col1, col2, col3) 
VALUES 
('foo', 'bar', 'alpha') 

मान लें कि col1 आपकी प्राथमिक कुंजी है, यदि मान 'foo' वाली पंक्ति पहले से मौजूद है, तो यह अन्य दो कॉलम अपडेट करेगी। अन्यथा यह एक नई पंक्ति डालेंगे।

+0

के साथ समस्या के साथ उपलब्ध है 'प्रतिस्थापित करें 'यह मानता है कि प्रविष्टि मौजूद है। लेकिन उपयोग डालने या अपडेट करना चाहता है - दोनों। – kasavbere

+1

यह खराब नाम है। यह मानता है कि प्रविष्टि मौजूद नहीं है। MySQL दस्तावेज़ों से: "REPLACE बिल्कुल INSERT की तरह काम करता है, सिवाय इसके कि यदि तालिका में पुरानी पंक्ति के पास प्राथमिक कुंजी या एक अद्वितीय सूचकांक के लिए एक नई पंक्ति के समान मूल्य है, तो नई पंक्ति डालने से पहले पुरानी पंक्ति हटा दी जाती है। " – bobwienholt

+0

मुझे यह नहीं पता था। आपको वोट दें - मैं आज कुछ नया सीखता हूं! – kasavbere

6

आप "सम्मिलित करें ... नकली चाबी अद्यतन पर" का उपयोग करने की कोशिश कर सकते वाक्य रचना

http://dev.mysql.com/doc/refman/5.0/en/insert-on-duplicate.html

If you specify ON DUPLICATE KEY UPDATE, and a row is inserted that would cause a duplicate value in a UNIQUE index or PRIMARY KEY, an UPDATE of the old row is performed. For example, if column a is declared as UNIQUE and contains the value 1, the following two statements have identical effect: 

INSERT INTO table (a,b,c) VALUES (1,2,3) 
    ON DUPLICATE KEY UPDATE c=c+1; 

UPDATE table SET c=c+1 WHERE a=1; 
The ON DUPLICATE KEY UPDATE clause can contain multiple column assignments, separated by commas. 

With ON DUPLICATE KEY UPDATE, the affected-rows value per row is 1 if the row is inserted as a new row and 2 if an existing row is updated. 

If column b is also unique, the INSERT is equivalent to this UPDATE statement instead: 

UPDATE table SET c=c+1 WHERE a=1 OR b=2 LIMIT 1; 
If a=1 OR b=2 matches several rows, only one row is updated. In general, you should try to avoid using an ON DUPLICATE KEY clause on tables with multiple unique indexes. 

You can use the VALUES(col_name) function in the UPDATE clause to refer to column values from the INSERT portion of the INSERT ... UPDATE statement. In other words, VALUES(col_name) in the UPDATE clause refers to the value of col_name that would be inserted, had no duplicate-key conflict occurred. This function is especially useful in multiple-row inserts. The VALUES() function is meaningful only in INSERT ... UPDATE statements and returns NULL otherwise. Example: 

INSERT INTO table (a,b,c) VALUES (1,2,3),(4,5,6) 
    ON DUPLICATE KEY UPDATE c=VALUES(a)+VALUES(b); 
That statement is identical to the following two statements: 

INSERT INTO table (a,b,c) VALUES (1,2,3) 
    ON DUPLICATE KEY UPDATE c=3; 
INSERT INTO table (a,b,c) VALUES (4,5,6) 
    ON DUPLICATE KEY UPDATE c=9; 
If a table contains an AUTO_INCREMENT column and INSERT ... UPDATE inserts a row, the LAST_INSERT_ID() function returns the AUTO_INCREMENT value. If the statement updates a row instead, LAST_INSERT_ID() is not meaningful. However, you can work around this by using LAST_INSERT_ID(expr). Suppose that id is the AUTO_INCREMENT column. To make LAST_INSERT_ID() meaningful for updates, insert rows as follows: 

INSERT INTO table (a,b,c) VALUES (1,2,3) 
    ON DUPLICATE KEY UPDATE id=LAST_INSERT_ID(id), c=3; 
The DELAYED option is ignored when you use ON DUPLICATE KEY UPDATE. 
6

आपको केवल डुप्लिकेट पर col3 को अपडेट करने की आवश्यकता है। यह वही है जो आप पूछ रहे हैं।

INSERT INTO models (col1, col2, col3) 
VALUES ('foo', 'bar', 'alpha') 
ON DUPLICATE KEY UPDATE col3='alpha'; 
संबंधित मुद्दे