473,802 Members | 1,978 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

UPDATE multiple rows in mysql (in one single query)

bilibytes
128 New Member
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 8048

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

Similar topics

2
2578
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> The above is an example of some data I am displaying in a form on an ASP page. I need to allow the users to edit any of the rows displayed and submit the changes back to
4
6141
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 click a save button to UPDATE the SQL table. I'd like to use stored procedures if possible. How is this done? Where do I start? thanks
4
16759
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 customers; I can execute each statement individually but get the 'you have an error in
10
13014
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 ImageFront=NULL, ImageFrontSize=NULL, ImageFrontType=NULL, ImageBack=NULL, ImageBackSize=NULL, ImageBackType=NULL WHERE AIM.AIMRetrievedItem.ArchiveRetrievalID = (SELECT
3
49561
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
1178
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 false), user will have an option to make another photo as active. when this happens newly selected photo should be set to 'True' and rest of the photos to 'False'
2
1892
by: aishah | last post by:
hi can anyody help me in giving and example of multiple queries and single query.
7
4592
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 rows and then update with one submit button. I can retrieve the rows and populate the form but the update does not work. Thanks in advance Here is the testing code obtained from http://www.phpeasystep.com/mysql/10.html
1
1514
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 ='GAT' & SET MTO_TEST_PDTABLE_34..MODEL_CODE = 'CONC_RED' WHERE MTO_TEST_PDTABLE_34..MODEL_CODE ='REDC'
0
9699
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9562
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10305
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10285
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10063
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9115
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7598
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6838
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
4270
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.