简体   繁体   English

脚本从mysql中的表转储单个记录

[英]script to dump single record from table in mysql

I want to dump single record from mysql table named as table1 for id = 5. 我想从名为table1的mysql表转储id = 5的单个记录。

What is mysql query for same? 什么是mysql查询相同?

I think we can use mysqldump but i have used it with full records of table. 我想我们可以使用mysqldump但我已经将它与表的完整记录一起使用了。 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. 如果您查看http://dev.mysql.com/doc/refman/5.0/en/mysqldump.html,您会发现 - where选项是您需要的选项。

Below follows what is stated in that reference: 以下是该参考文献中所述的内容:

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

Dump only rows selected by the given WHERE condition. 仅转储由给定WHERE条件选择的行。 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); 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: 事实上,如果我没有弄错,甚至可以在mysql中的不同服务器上插入select数据库,方法是在每个数据库名称和表的前面添加服务器名称,如下所示:

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" . 使用此查询“SELECT * FROM table1 WHERE id ='5'LIMIT 1”。 Should work fine. 应该工作正常。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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