my issue is this
I have table in my database which have more than 1 million rows. Sometimes i need have sql dump of my table in my PC and export whole table taking very long.
Actually i need exported table for example with last 5000 rows.
So is there a way to export MySql table by selecting last X rows.
I know some ways to do it by terminal commands, but i need poor MySql query if it is possible.
Thanks
Asked
Active
Viewed 2,595 times
0

Hayk Abrahamyan
- 100
- 1
- 10
-
Use `order by` and `limit` – juergen d Jan 07 '18 at 10:25
-
Sorry, but i not get at all, i need query to export sql dumb by limit and not by shell command – Hayk Abrahamyan Jan 07 '18 at 10:29
-
there is not any part to export – Hayk Abrahamyan Jan 07 '18 at 10:31
-
https://stackoverflow.com/a/135900/575376 – juergen d Jan 07 '18 at 10:32
-
my question about MySql query and not shell command, i want to use query from phpMyadmin / SqlYog or by other tools where i can run query, without shell command – Hayk Abrahamyan Jan 07 '18 at 10:35
-
So a simple SQL query? Come on. This is very basic SQL. You should take a SQL tutorial. `select * from your_table order by a_column_that_indicates_an_order desc limit 5000` – juergen d Jan 07 '18 at 10:51
1 Answers
0
If I understand well, you could try by using the INTO OUTFILE
functionality provided by MySQL. of course I don't know what's your current query but you can easily change my structure with yours:
SELECT *
FROM table_name
INTO OUTFILE '/tmp/your_dump_table.csv'
FIELDS TERMINATED BY ','
ENCLOSED BY '"'
LINES TERMINATED BY '\n'
ORDER BY id DESC
LIMIT 5000
Since the user @Hayk Abrahamyan has expressed preference to export the dump as .sql file, let's try to analyze a valid alternative:
- We can run the query from phpmyadmin or (it's for sure a better solution) MysqlWorkbench SQL editor console and save it by press the export button (see the picture below):
As .sql output file result you will have something like the structure below:
/*
-- Query: SELECT * FROM mytodo.todos
ORDER BY id DESC
LIMIT 5000
-- Date: 2018-01-07 13:15
*/
INSERT INTO `todos` (`id`,`description`,`completed`) VALUES (3,'Eat something',1);
INSERT INTO `todos` (`id`,`description`,`completed`) VALUES (2,'Buy something',1);
INSERT INTO `todos` (`id`,`description`,`completed`) VALUES (1,'Go to the store',0);

Giulio Bambini
- 4,695
- 4
- 21
- 36
-
thank you Giulio, but is there way to export as .sql file, that is what i am looking for – Hayk Abrahamyan Jan 07 '18 at 10:52
-
@HaykAbrahamyan I have updated my answer with an extra different approach. Since for what I can see, there is not a real way to export .sql file without using `mysqldump`by command line. You can alternatively think about for create an `INSERT INTO` statement in another new empty table inside your current or in a new database schema (so, without store the dump data in an external file). – Giulio Bambini Jan 07 '18 at 12:24
-