MySQL5: Delete all but the 50 newest rows?

Blockquote delete from table where id not in ( select id from table order by id desc limit 50 ) You select the ids of the data you don't want to delete, and the you delete everything NOT IN these value.

Blockquote delete from table where id not in ( select id from table order by id desc limit 50 ) You select the ids of the data you don't want to delete, and the you delete everything NOT IN these value...

This assumes you have sequential ID's, which is usually a fair assumption. If you don't though, you'll need to make use of a timestamp or some other sequential field in your DB to do this – Orion Edwards Sep 28 '08 at 19:33 Doesn't seem to work, just get the error: #1235 - This version of MySQL doesn't yet support 'LIMIT & IN/ALL/ANY/SOME subquery' – Gilean Sep 28 '08 at 19:37 Surely the subquery should be "select id from table", not "select * from table"? – Blorgbeard Sep 28 '08 at 19:38 @Gilean you should tune this query and see what kind of commands your mysql version supports.

This was only an input for your question... – Davide Vosti Sep 28 '08 at 19:40 Looks like LIMIT isn't supported in subqueries in MySQL 5 – Gilean Sep 28 '08 at 19:56.

Well, it sort of looks like you can't do it in one query - someone correct me if I'm wrong. The only way I've ever been able to do this sort of thing is to first figure out the number of rows in the table i.e. Select count(*) from table; then using the result do delete from table order by timestamp limit result - 50; You have to do it this way for two reasons - MySQL 5 doesn't support limit in subqueries for delete MySQL 5 doesn't allow you to select in a subquery from the same table you are deleting from.

I ended up using two queries since MySQL5 doesn't yet support LIMIT's in subqueries SELECT unixTime FROM entries ORDER BY unixTime DESC LIMIT 49, 1; DELETE FROM entries WHERE unixTime.

If you have a lot of rows, it might be better to put the 50 rows in a temporary table then use TRUNCATE TABLE to empty the table out. Then put the 50 rows back in.

Truncate resets the auto increment counter - so if your IDs are on auto-increment you would need to modify the next value of auto-increment after truncating the table so that the next ID comes after the highest value of the data you kept. – Max Stewart Sep 28 '08 at 20:13 Nice comment, thanks for pointing that out. – EvilTeach Dec 5 '08 at 2:28.

Assuming this query selects the rows you want to keep: SELECT timestampcol FROM table ORDER BY timestampcol DESC LIMIT 49,1; Then you could use a subquery like so: DELETE FROM table WHERE timestampcol.

This doesn't work (#1093 - You can't specify target table 'entries' for update in FROM clause). See stackoverflow. Com/questions/45494/… – Gilean Sep 28 '08 at 19:35.

Maybe not the most efficient, but this should work: DELETE FROM _table_ WHERE _date_ NOT IN (SELECT _date_ FROM _table_ ORDER BY _date_ DESC LIMIT 50).

Doesn't seem to work, MYSQL5 doesn't support LIMIT in subqueries: #1235 - This version of MySQL doesn't yet support 'LIMIT & IN/ALL/ANY/SOME subquery – Gilean Sep 28 '08 at 20:00.

I'd like to only keep the 50 newest stories. How would I write an SQL statement to delete any amount of older stories?

I was not aware that Nioxin had changed bottles or formulas, however it sounds like they may have. If you purchased it from Amazon, you can rest asure the product is authentic. If they were to sell a knock off, they would clearly state that in the description.

Amazon is a great place to buy Nioxin. I actually buy Nioxin at Amazon, as well. As I mentioned, I have not noticed a change in any of my products, but I really would not be alarmed.

You can always contact Nioxin directly, with any concerns. You can let them know the exact product this is in regards to and they can then confirm that it is authentic. Their customer service is great, so if you are unsure, feel comfortable calling them.

Have you used the new product? Does it seem to be working the same? You may also call Amazon.

They are good about returns. Their customer service is great, as well.

I also noticed a difference from the Nioxin System 1 set I bought on Amazon from an Amazon store called 'Beauty in Style'. The conditioner is not as green, not as minty, more 'topical-cream-like' and not as viscous, meaning it's harder to rub thru the hair. Worst of all, it clearly doesn't work as well for me and has me worried I bought a fake.

After using this for so many years, I'm very disappointed.

I was not aware that Nioxin had changed bottles or formulas, however it sounds like they may have. If you purchased it from Amazon, you can rest asure the product is authentic. If they were to sell a knock off, they would clearly state that in the description.

Amazon is a great place to buy Nioxin. I actually buy Nioxin at Amazon, as well. As I mentioned, I have not noticed a change in any of my products, but I really would not be alarmed.

You can always contact Nioxin directly, with any concerns. You can let them know the exact product this is in regards to and they can then confirm that it is authentic. Their customer service is great, so if you are unsure, feel comfortable calling them.

Have you used the new product? Does it seem to be working the same? You may also call Amazon.

They are good about returns. Their customer service is great, as well. I was not aware that Nioxin had changed bottles or formulas, however it sounds like they may have.

If you purchased it from Amazon, you can rest asure the product is authentic. If they were to sell a knock off, they would clearly state that in the description. Amazon is a great place to buy Nioxin.

I actually buy Nioxin at Amazon, as well. As I mentioned, I have not noticed a change in any of my products, but I really would not be alarmed. You can always contact Nioxin directly, with any concerns.

You can let them know the exact product this is in regards to and they can then confirm that it is authentic. Their customer service is great, so if you are unsure, feel comfortable calling them. Have you used the new product?

Does it seem to be working the same? You may also call Amazon. They are good about returns.

Their customer service is great, as well.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions