How mysql updates multiple records at a time

Source: Internet
Author: User
Tags float number

A problem occurs at work: update a data table.

This table is created by myself and has seven fields: id, name, package, etc.

During creation, because I saved the name and package information in two text files,

So I will use the Insert method to Insert all the names into the database at a time.

All the names are imported into the database, but my package is not imported. In this case, I still want to use the insert method, but not.

In this case, the update method should be used. The idea of updating multiple pieces of information at a time is as follows:

UPDATE table_name SET field_name = CASE other_field WHEN 1 THEN 'value' WHEN 2 THEN 'value' WHEN 3 THEN 'value' end where id IN (1, 2, 3) the test code is as follows: <? Php/** function: insert app's apk, logo_url, document_title, app_desc, package_name * into talbe atable use database db. * // connect database catx. $ server = 'localhost'; $ user = 'root'; $ passwd = 'root'; $ port = '000000'; $ dbname = 'catx '; $ link = mysql_connect ($ server, $ user, $ passwd); if (! $ Link) {die ('could not connect :'. mysql_error ();} else echo "Connected successfully \ n"; mysql_select_db ("db", $ link); // set init variable and start time $ st = microtime_float (); $ table = "pydot_g"; $ path = "txt"; $ fname_package_name = "package_name.txt"; // $ handle = @ fopen ($ path. "/". $ fname_package_name, "r"); $ I = 1; $ SQL = "UPDATE pydot_g SET package_name = CASE id"; $ ids = ""; while ($ buf [$ I] = fgets ($ Handle, 512 ))! = False) {$ SQL. = sprintf ("WHEN % d THEN '% S'", $ I, $ buf [$ I]); // concatenate an SQL statement $ ids. = sprintf ("% d,", $ I); $ I ++;} // $ ids = implode (',', $ ids); $ ids. = $ I; $ SQL. = "end where id IN ($ ids)"; echo $ SQL; mysql_query ($ SQL); fclose ($ handle); mysql_close ($ link ); // echo the results and total time used $ et = microtime_float (); $ t = $ et-$ st; echo "\ r \ ninsert into talbe", $ table, "", $ I, "times; \ r \ n"; echo "Total time $ t sec Onds. \ r \ n "; // function calculate time, return a float number function microtime_float () {list ($ usec, $ sec) = explode (" ", microtime ()); return (float) $ usec + (float) $ sec) ;}?>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.