简体   繁体   中英

script to dump single record from table in mysql

I want to dump single record from mysql table named as table1 for id = 5.

What is mysql query for same?

I think we can use mysqldump but i have used it with full records of table. But how to use it with single record?

if you check at http://dev.mysql.com/doc/refman/5.0/en/mysqldump.html you will find --where option which is the one you need.

Below follows what is stated in that reference:

--where='where_condition', -w 'where_condition'

Dump only rows selected by the given WHERE condition. Quotes around the condition are mandatory if it contains spaces or other characters that are special to your command interpreter.

Examples:

--where="user='jimf'"
-w"userid>1"
-w"userid<1"

Although the below will not work for a new/different database

Try

INSERT INTO table2 (SELECT * FROM table1 WHERE id = 5);

All one needs to do if inserting across databases on the same server is:

INSERT INTO `newdbname`.`table2` (SELECT * FROM `olddbname`.`table1` WHERE id = 5);

In fact if I'm not mistaken one can even insert select across databases on different servers in mysql by adding the server name infront of each database name and table as follows:

INSERT INTO `localhost`.`newdbname`.`table2` (SELECT * FROM `accessible-live-server-name`.`olddbname`.`table1` WHERE id = 5);

insert into [dbo].[LocaleStringResource]
  (LanguageId,ResourceName,ResourceValue)
select 5 as LanguageId, ResourceName, ResourceValue
from [dbo].[LocaleStringResource]
where LanguageId = 2 and id = (
  SELECT MAX(ID)
  FROM [dbo].[LocaleStringResource]
)

I just would like to post a more explicit answer. This is complementary to the accepted one.

mysqldump -uremote_user -premote_password -hdatabase_host remote_database_name remote_table_name --where='id=1234' --skip-add-drop-table --no-create-info

尝试

INSERT INTO table2 (SELECT * FROM table1 WHERE id = 5);

Use this query "SELECT * FROM table1 WHERE id='5' LIMIT 1" . Should work fine.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM