Go Back   CodingForums.com > :: Server side development > MySQL

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 03-05-2009, 01:34 AM   PM User | #1
millsy007
New Coder

 
Join Date: Feb 2009
Posts: 52
Thanks: 0
Thanked 0 Times in 0 Posts
millsy007 is an unknown quantity at this point
Move Data In a Database Table

I want to do a query to move data within a table.

I am passing in a variable that is the column that I want the data to go into.
I also know the data that I need to move.

So If there was a table called Film, with the following values:

Actor, Director, Producer
Tarantino

and in my query I was passing in
$role = director
and
$person = tarantino

ie I wanted the data to move from the actor column to the director for this example

How could I do this in sql/code?

millsy007 is offline   Reply With Quote
Old 03-05-2009, 02:14 AM   PM User | #2
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,556
Thanks: 62
Thanked 4,056 Times in 4,025 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
Sorry...that doesn't make sense.

You show *THREE* columns: Actor, Director, Producer

You show a VALUE for only ONE column: Tarantine

Which column is "Tarantino" in ???

And do you want to move *ALL* records which match your given criteria or just one row?
Old Pedant is offline   Reply With Quote
Old 03-05-2009, 11:09 AM   PM User | #3
millsy007
New Coder

 
Join Date: Feb 2009
Posts: 52
Thanks: 0
Thanked 0 Times in 0 Posts
millsy007 is an unknown quantity at this point
Sorry that wasnt very clear, for this example the data (tarantino) is in the Actor Column.

However what i want is to say is wherever the value 'tarantino' is in another column it should now be in the $role column.

So
Actor, Director, Producer
Tarantino

Should Become
Actor, Director, Producer
Tarantino

After the query has run
And yes this would be for all records
millsy007 is offline   Reply With Quote
Old 03-05-2009, 11:07 PM   PM User | #4
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,556
Thanks: 62
Thanked 4,056 Times in 4,025 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
Apparently you are typing in spaces, thinking that will show us what field a value is in. But spaces are IGNORED in HTML, so we just see what you would see if you looked at your own post.

To preserve spaces, wrap those parts of your post in [ code ] .... [ /code ] tags.

I am *GUESSING* that you mean:
Code:
EXISTING RECORD:
Actor      Director     Producer
xx         xx           Tarantino

Then *IF* the fields given are:
$role = director
and
$person = tarantino

THAT SHOULD BECOME:
Actor    Director    Producer
xx       Tarantino   Tarantino
Yes??? You don't say what to do with the field that the name was moved *FROM*, so I'm just leaving it as is.

So... let's think about it...
Code:
UPDATE table
SET $role = '$person'
WHERE $role != '$person'
AND '$person' IN ( Actor, Director, Producer )
I think that works. It first looks for all records where 'Tarantino' is *NOT* the Director and then for all records where 'Tarantino' *IS* either actor or director or producer. (Naturally, the fact that we check director in the second test is essentially ignored because of the first test.) When it finds such a record, it changes the Director to be, as you asked, 'Tarantino'.
Old Pedant is offline   Reply With Quote
Old 03-06-2009, 04:27 PM   PM User | #5
guelphdad
Super Moderator


 
guelphdad's Avatar
 
Join Date: Mar 2006
Location: St. Catharines, Ontario Canada
Posts: 2,629
Thanks: 4
Thanked 147 Times in 138 Posts
guelphdad will become famous soon enoughguelphdad will become famous soon enough
actually you should have two columns, 1 column for the name and the other denoting the roll. if tarantino is a producer, director and actor he should have three rows in the table.

normalize your data is always the best bet!
guelphdad is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 06:07 PM.


Advertisement
Log in to turn off these ads.