Jump to content

[SOLVED] change one field of one row to one value, every other row a different value


Recommended Posts

I have a MySQL table like so:

 

CREATE TABLE IF NOT EXISTS `paul_pages` (
  `page_id` int(10) unsigned NOT NULL auto_increment,
  `page_title` varchar(128) NOT NULL,
  `page_content` text NOT NULL,
  `page_use_meta` tinyint(1) NOT NULL default '0',
  `page_default` tinyint(1) NOT NULL default '0',
  PRIMARY KEY  (`page_id`)
) ENGINE=MyISAM  DEFAULT CHARSET=utf8 AUTO_INCREMENT=4 ;

 

What I want to do is if page_id of the row is equal to $page_id, I'll change page_default to 1, but for every other row change it to 0. Can that be done in one query or do I have to do it with 2 queries?

the mysql manual is your friend:

 

http://dev.mysql.com/doc/refman/5.0/en/control-flow-functions.html

 

it might be faster for you to simply run two queries: in the first, set all page_defaults to 0, then update only the row with the given page_id to have page_default of 1.

So you want to use PHP to cycle through the table and check table.page_id = $page_id and if so Update page_default = 1 else page_default = 0?

 

No. This is sort of what I want to do in the query:

 

UPDATE `paul_pages`
IF `page_id` = 3 THEN SET `page_default`=1
ELSE SET `page_default`=0
END IF

I suppose you have only one row with default = 1 so this might work

 

UPDATE `paul_pages` SET `page_default` = 1 - `page_default` WHERE `page_id` IN ($oldDefaultID, $newDefaultID)

 

you need to know $oldDefaultID though

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.