MySQL:如何在SQL查询中为每个结果插入一条记录? [英] MySQL: How to insert a record for each result in a SQL query?

查看:368
本文介绍了MySQL:如何在SQL查询中为每个结果插入一条记录?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

说我有选择

SELECT DISTINCT id, customer_id, domain FROM config WHERE type = 'foo';

返回一些记录.

如何在结果集中插入插入行

How can I do an insert for reach row in the result set like

INSERT INTO config (id, customer_id, domain) VALUES (@id, @customer_id, 'www.example.com');

@id@customer_id是结果集中行的字段吗?

where @id and @customer_id are the fields of the row in the result set?

我不想只复制它,而是在domain字段中插入一个新值.尽管如此,因为它很简单,所以很容易找到一个脸部表情;-)谢谢!

edit: I didn't want to just duplicate it, but insert a new value in the field domain instead. Nevertheless a facepalm-situation as it's plain easy ;-) Thanks!

推荐答案

就这么简单:

INSERT INTO config (id, customer_id, domain) 
SELECT DISTINCT id, customer_id, domain FROM config;

如果要将"www.example.com"作为域,则可以执行以下操作:

If you want "www.example.com" as the domain, you can do :

INSERT INTO config (id, customer_id, domain) 
SELECT DISTINCT id, customer_id, 'www.example.com' FROM config;

这篇关于MySQL:如何在SQL查询中为每个结果插入一条记录?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆