Solved

mysql query for alternating update

Posted on 2012-03-27
3
268 Views
Last Modified: 2012-03-27
I need to update a table so that the field 'series' alternates between 'a' and 'b'.

Something like this:
------------
include ("/home/mysite/connect.inc");
mysql_select_db(oys) or die("Unable to load update into mysql");

$query = "UPDATE oys_sites SET `series`=

//at this point, I need the 'series' field in the first row set to 'a', the next row set to 'b', then 'a', then 'b', alternating, until all rows are done.
-------

This will be entered into a php script so I can set it when needed.

Thanks for any help writing this up,

Chris
0
Comment
Question by:St_Aug_Beach_Bum
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 83

Accepted Solution

by:
Dave Baldwin earned 500 total points
ID: 37774736
You haven't specified an ORDER BY so you can't tell which rows are getting what.  Any deletions and inserts will alter the 'natural' order of the rows in the table because SQL databases don't preserve that order.
0
 

Author Comment

by:St_Aug_Beach_Bum
ID: 37775121
Ok, I've got it

Worked it out by building a loop and incrementing through it and updating one row at a time, using the increment count as a way to switch back and forth between 'a' and 'b'.

Thanks for the suggestion Dave, I added a note here to say I wasn't quite following you on that and to say it was existing entries I was trying to affect - but that note didn't seem to show up here. Maybe I messed something up.

Thank you for the attempt though and I will award you points for trying to help :)  

Chris
0
 
LVL 83

Expert Comment

by:Dave Baldwin
ID: 37775233
Since you posted an UPDATE statement, I assumed existing entries.  What I was trying to point out was that without using an ORDER BY statement to set the order, you are unable to 'guarantee' that the alternating order will be preserved in the future.  On the other hand, if the table isn't too big, you can just run that code again to reset it.
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
This article discusses how to create an extensible mechanism for linked drop downs.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

728 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question