Jump to content

update oldest row with certain value inside of it to a different value


Russia

Recommended Posts

Now im trying to do one more thing, that if I make another row with the value 2 for the type of news it is, I want to automatically turn the oldest news with the value 2 in the type column to '3'.

 

Is there any kind of query to update the row to '3' if a row is found with the same value and update the oldest news that was posted with the value 2. Also, I want it to work that if only there are 3 rows in there with the same value.

 

Id be happy if I could get some help.

 

Is there anything that can do it like that? or can that not be created?

 

 

Link to comment
Share on other sites

I don't get what you're asking. You want to update the oldest 2 to a 3 as soon as you add a new 2 row?

 

//  do whatever you need to insert your row

if ($value==2) {
     $row = mysql_fetch_array(mysql_query('SELECT id FROM table WHERE value=2 ORDER BY id LIMIT 1'));
     $id=$row['id'];
     mysql_query('UPDATE table SET value=3 WHERE id='.$id.' LIMIT 1');
}

Link to comment
Share on other sites

Yeah im trying to find the oldest row created that has a value for that row as 2 in that column named 'newstype', it has a timestamp on it in the column named 'dtime'

 

Im trying to get that row, then set that column named 'newstype' value from 2 to 3.

 

Is that possible?

 

 

 

 

Link to comment
Share on other sites

Okay, but the thing is, I dont want to set a date, I want it to automatically find the oldest date. Should I just set the date to like the 1900's just as a failsafe? Also, it will edit the OLDEST row only, not all the rows that are before that date correct?

 

Again I dont want to set a date I want it to find the row with the oldest date, and update it to 3.

 

 

Link to comment
Share on other sites

Do you want to update the oldest row or all rows before that date?

 

If you just want the oldest row:

$row = mysql_fetch_array(mysql_query('SELECT dtime FROM table WHERE newstype=2 ORDER BY dtime LIMIT 1'));
$time=$row['dtime'];
mysql_query('UPDATE table SET newstype=3 WHERE newstype=2 AND dtime = '.$time.' LIMIT 1');

Link to comment
Share on other sites

I always want to keep three of the news that are value 2. When I create a new news post with the value 2 I want to update the oldest one to 3.

 

Basically the 2 will mean that the 3 current news that arent important.

 

1 will stay for a long time until I create a new MAIN news post which will set the old MAIN news post before it to 2.

 

Do you kind of understand it now?

 

Also, how do I do it so it only updates it when there are exactly 3 rows that contain the value 2, so i can keep exactly 3

Link to comment
Share on other sites

I'm betting there's a much less convoluted way of doing whatever it is you're trying to do. Why don't you explain to us what the overall goal you want to achieve here is. I.E. why are you trying to assign these 'flag' numbers to the different records, and what will they be used for?

Link to comment
Share on other sites

Im trying to keep at any one time i want to keep 3 rows with the value 2 in the news_type column. If a new news post is made with the value 2 in the news_type column I want to make it so the oldest news posted with the value 2 in the column news_type to be set to 3. So that there aren't 4 rows with the value 3 but still 3 just with a new news post.

 

I want it to also only update it to 3 if there are ONLY 3 rows (no less than 3 rows or more than 3 rows) with the value 2 in column news_type.

 

(took me a while to actually try and explain it in a sensible format without to many words).

 

Link to comment
Share on other sites

For a news system, there are 3 sections.

 

MAIN NEWS (contains the only row with the value 1 in the column 'news_type'.)

 

RECENT NEWS (contains the 3 rows that contain the value 2 in the column 'news_type'.)

 

when a new row is created with the value 2 the oldest row with the value 2 getting updated with the value 3. turning it into an archives and showing it in the archives section.

 

ARCHIVES (contains the rows that have the row value 3 in the column 'news_type'.)

 

 

 

Link to comment
Share on other sites

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.