5

I'm using the following command to dump 100 rows from each column from each database. The problem is that it returns the first 100 records where I would much rather have the last. I found a flag that lets me order by primary key (--order-by-primary), but that still returns the wrong rows. I would try to sneak an 'order by' into the where flag below, but the primary key is different for each table.

mysqldump -u username -p --where="true limit 100" --all-databases > dump.sql

Is there a way to reverse the order before grabbing the last 100 records or perhaps a way of referring to the PK rather than the PK's column name?

3
  • 2
    Not really. But you can order on the fields: order by 1,2,3,4,5. If your PK always consists of the first (x) fields, this works. If your PK is 1,3,5, it won't :)
    – Konerak
    Mar 22, 2011 at 16:59
  • Still interested? I have a linux-version with that revert&limit, it was that easy. It would be useful if the primary key is not the first column for all tables. I try to make a real patch with commandline-options for those things, but not yet sure how to publish it since it needs some mysql-client-libs. May 5, 2013 at 15:17
  • I try to publish it as MySQL Feature Request 69163, we'll see. May 7, 2013 at 16:22

2 Answers 2

0

I think as long as you don't want to patch the mysqldump source code there is just a workaround.

Try creating a secondary database schema (scheme_dump) and make a script which loops over all tables, fires

create table scheme_dump.<tabname> like <tabname>; 
insert into scheme_dump.<tabname> select * from <tabname> order by <colname> desc limit 100;

and then dump scheme_dump scheme.

Not nice.

But fixing the source code for a one-shot should not be that hard.

-1

May be this : SELECT * FROM TableName ORDER BY DESC LIMIT 100

1
  • that's a flat out syntax error. desc is a reserved word, and you can't have a blank order by clause anyways.
    – Marc B
    May 3, 2013 at 20:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.