使用重复数据自动插入行,遵循两种模式

时间:2016-01-27 15:21:33

标签: mysql sql mariadb

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

| id  | letter | number |
|-----|--------|--------|
| 1   | a      | 1      |
| 2   | b      | 1      |
| 3   | c      | 1      |
| 4   | d      | 1      |
| 5   | a      | 2      |
| 6   | b      | 2      |
| 7   | c      | 2      |
| 8   | d      | 2      |
| 9   | a      | 3      |
| 10  | b      | 3      |
| 11  | c      | 3      |
| 12  | d      | 3      |
|etc..|        |        |

我试图制作一个SQL语句,按照此模式自动填充表格,最高为id 456。

所以这些字母都是ABCD ABCD,直到序列结束,并且每个'组' 4的数字应该达到114。

我不确定解决这个问题的最佳方法是什么,任何建议都会受到赞赏。

2 个答案:

答案 0 :(得分:2)

您可以使用以下sql脚本在表中插入所需的值:

apply plugin: 'com.android.application'

android {
compileSdkVersion 23
buildToolsVersion '23.0.2'
defaultConfig {
    applicationId "com.pipperpublishing.refwatch"
    minSdkVersion 21
    targetSdkVersion 23
    versionCode 203
    versionName "0.2.03"
}
buildTypes {
    release {
        minifyEnabled false
        proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
    }
    debug {
        debuggable true
    }
}
}

dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.google.android.support:wearable:1.3.0'
compile 'com.android.support:recyclerview-v7:23.1.1'
compile 'com.android.support:support-v13:23.1.1'
compile 'com.google.android.gms:play-services-wearable:8.4.0'
compile 'com.google.android.gms:play-services-auth:8.4.0'
}
apply plugin: 'com.google.gms.google-services'    

上述查询使用11 x 11笛卡尔积创建了121行的数字表。这些行与内联表INSERT INTO target (id, letter, `number`) SELECT rn, col, (rn - 1) % 4 + 1 AS seq FROM ( SELECT col, @rn := @rn + 1 AS rn FROM ( SELECT 'a' AS col UNION ALL SELECT 'b' UNION ALL SELECT 'c' UNION ALL SELECT 'd') AS t CROSS JOIN ( SELECT 1 AS x UNION ALL SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1 ) AS t1 CROSS JOIN ( SELECT 1 AS x UNION ALL SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1 ) AS t2 CROSS JOIN (SELECT @rn := 0) AS var ) AS s WHERE rn <= 456 交叉连接,以产生总共484行。外部查询仅选择所需的行,即总共456行。

注意:如果要插入值:

('a'), ('b'), ('c'), ('d')

而不是值:

id, letter, number
1   'a'     1
2   'b'     1
3   'c'     1
4   'd'     1
5   'a'     2
6   'b'     2
7   'c'     2
8   'd'     2
... etc

然后只需将id, letter, number 1 'a' 1 2 'b' 2 3 'c' 3 4 'd' 4 5 'a' 1 6 'b' 2 7 'c' 3 8 'd' 4 ... etc 替换为(rn - 1) % 4 + 1 AS seq

Demo here

答案 1 :(得分:0)

如果您有某种数字表会有所帮助。这是使用cross join和一些算术的一种方法:

select (@rn := @rn + 1) as id, l.letter, (n1 + n2*5 + n3*25) as number
from (select 0 as n union all select 1 as n union all select 2 as n union all select 3 union all select 4
     ) n1 cross join
     (select 0 as n union all select 1 as n union all select 2 as n union all select 3 union all select 4
     ) n2 cross join
     (select 0 as n union all select 1 as n union all select 2 as n union all select 3 union all select 4
     ) n3 cross join
     (select 'a' as letter union all select 'b' union all select 'c' union all select 'd'
     ) l cross join
     (select @rn := 0) params
where n1 + n2*5 + n3*25 < 114;