15

I need to export the data and structure of a table but the data must have a specific condition (WHERE status=0 and id>20).

How to export mysql database based on a where condition from phpMyAdmin or anything.

asked Mar 21, 2012 at 11:13

3 Answers 3

17

Using SQL from the mysql command-line:

SELECT * from YOURTABLE
WHERE status=0 and id>20
INTO OUTFILE 'yourtable.csv'
FIELDS TERMINATED BY ','
ENCLOSED BY '"'
LINES TERMINATED BY '\n';

or using mysqldump with the --where= option:

mysqldump -u youruser -p yourdbname yourtablename --where="status=0 and id>20">yourtable.sql

Using phpMyAdmin you can execute the query in the GUI & click "export" under the resultset.

answered Mar 21, 2012 at 11:24
1

In my case from table_name .... before INTO OUTFILE gives an error(Unexpected ordering of clauses. (near "FROM" at position 10)).

What works for me.

SELECT *
INTO OUTFILE '/Volumes/Development/sql/sql/enabled_contacts.csv'
FIELDS TERMINATED BY ','
ENCLOSED BY '"'
LINES TERMINATED BY '\n' 
FROM table_name
WHERE column_name = 'value'
answered Sep 21, 2020 at 10:15
0
  • Solution 1: use mysqldump from command line
mysqldump --no-create-db --no-create-info --complete-insert \
 -u<yourUsername> -p<yourPassword> -h<yourDbHost> -P<yourDbPort> \
 <yourDatabaseName> <yourTableName> --where="status=0 and id>20"

Please be aware of that by default mysqldump will try to lock the table first, so in case you meet an error about lacking of privilege for locking table, probably you need --single-transaction also, see https://stackoverflow.com/questions/104612/run-mysqldump-without-locking-tables for not locking table for different db engines.

  • Solution 2: use a GUI client tool.

For example in MySQL Workbench, you can run the select and export the query result to a csv file. enter image description here

answered Jul 26, 2021 at 10:38

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.