473,395 Members | 1,613 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

UPDATE multiple rows in mysql (in one single query)

bilibytes
128 100+
hi,

i am trying to UPDATE multiple rows with mysql. I know how to do it with multiple queries but i think it would be less resource consuming generating mysql query code with php and update all one single step.

here is the method i usually employ:

$value_column_1 = array();
$value_column_2 = array();
Expand|Select|Wrap|Line Numbers
  1. for($i = 0; $i<= $number_of_updates; $i++){
  2.  
  3.    $query = "UPDATE table SET column_1 = '$value_column_1[$i]' WHERE
  4.    column_2 = '$value_column_2[$i]'";
  5.  
  6. }
  7.  
then i make the query and it works properly,
but i think it would be more efficient to make a query like the ones that can be made with
Expand|Select|Wrap|Line Numbers
  1. INSERT INTO table(column_1, column_2) VALUES ($value_column_1[$i], $value_column_2[$i])
...
making a "big" query:
Expand|Select|Wrap|Line Numbers
  1. $first_time = true; //first time is set to determine weather to put a ', ' or not 
  2. $q = "INSERT INTO table(column_1, column_2) VALUES";
  3. for($i = 0; $i<= $number_of_updates; $i++){
  4.    if($first_time == true){
  5.       $q = $q."($value_column_1[$i], $value_column_2[$i])";
  6.       $first_time = false;
  7.    }
  8.    else{
  9.       $q = $q.",($value_column_1[$i], $value_column_2[$i])";
  10.    }
  11. }
  12.  
  13. is there some thing similar with update?
  14.  
  15. thank you
  16.  
Sep 17 '08 #1
0 8025

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Colin Steadman | last post by:
Part No Description Quantity 45643 Random part 10 45678 Another Random part 7 98944 And another 1 <submit button> ...
4
by: shank | last post by:
Visually, the page will look somewhat like a spreadsheet. It could have hundreds of records (rows) displayed. I want to enable the user to edit any one or any number of records and any fields, then...
4
by: DG | last post by:
Hi, Can anyone advise how to execute multiple statements in a single query batch. For example- update customers set customer_name = 'Smith' where customer_name = 'Smyth'; select * from...
10
by: Anthony Robinison | last post by:
I'm trying to UPDATE multiple rows in a single statement. I've seen some examples but to me they don't seem to apply to my scenario. Below is my original SQL: UPDATE AIM.AIMRetrievedItem SET...
3
by: spartacus | last post by:
Hi, Does SQL support update to multiple rows where values coming from a sub-query? e.g insert into TABLE1 select column1, column2, column3 from TABLE2
0
by: sanju | last post by:
Hi, I have a table called Photos, where user will have 1 to 10 photos and only one photo will set to be active at any given point of time.(i.e IsPermitted ='True' and remaining all photos set to...
2
by: aishah | last post by:
hi can anyody help me in giving and example of multiple queries and single query.
7
by: CoachDave48 | last post by:
I am a rookie coder and got some great help from Marcus last year so I thought I would make another request. I am trying to retrieve all boys in one household into a form and edit the individual...
1
by: mohinder | last post by:
the following query is not working in ms acess, can you please help me? UPDATE MTO_TEST_PDTABLE_34 SET MTO_TEST_PDTABLE_34.MODEL_CODE = 'GATE_VALVE' WHERE MTO_TEST_PDTABLE_34.MODEL_CODE...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.