mysql:使用 SQL 复制某些行? [英] mysql: duplicate certain rows using SQL?

查看:30
本文介绍了mysql:使用 SQL 复制某些行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一张看起来像这样的表格:

I have a table that looks like this:

nid  vid  tid
1   2    3
2   2    4
3   2    4
4   2    3

我想运行一些 SQL 来获取这些行中的每一行并基于它创建另一个,如下所示:

I'd like to run some SQL that will take each one of those rows and create another based on it like so:

foreach row where vid=2,创建一个重复的 row where vid = 3

foreach row where vid=2, create a duplicate row where vid = 3

所以我会这样结束:

   nid  vid  tid
    1   2    3
    2   2    4
    3   2    4
    4   2    3
    1   3    3
    2   3    4
    3   3    4
    4   3    3

表不是那么大(我认为不到 500 行)而且这是一次性的(所以代码不需要进行惊人的优化或任何事情).谢谢!

The table is not that big (less than 500 rows, I think) and this is a one-time thing (so the code does not need to be amazingly optimized or anything). Thanks!

推荐答案

您能否执行以下操作:

INSERT INTO TheTable(nid, vid,  nid)
SELECT nid, 3, nid
FROM TheTable
WHERE vid = 2

两列似乎具有相同的名称,但以上应该有效.它需要一组 Vid = 2 元素,再次插入它们,但使用 Vid = 3.

Two columns appear to have the same name, but the above should work. It takes the set of Vid = 2 elements, inserts them again but using Vid = 3.

这篇关于mysql:使用 SQL 复制某些行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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