Insert语句中的多个选择 - mySQL

时间:2018-05-28 17:57:32

标签: mysql

我有2张桌子。

1

**code**
id
name

2

**code_category**
id
code_id
category_id
discount

我在数据库中没有code_category的15000个代码列表。我只有1个样本,可以在code_category中引用。我必须在code_category中大量添加每个代码20个类别。

我正在考虑左边连接两个表以获取code_id为null的位置,这样我可以获得所有未附加的代码,但我需要另一个select语句来获取每个代码输入的20种不同类型的code_category行。

insert into code_category (code_id, category_id, discount)
values
    (select id from code c
    left join code_category cc on cc.code_id = c.id
    where cc.code_id is null)
    union 
    (select categoryid, discount from code_category where c.id = 123)

123是参考ID。

1 个答案:

答案 0 :(得分:0)

我在SQL Fiddle中构建了一个简化的表格示例,然后只运行select(如下所示)以查看要插入的行。看看这个例子是否回答了你的问题。

请注意我在选择中如何离开连接两次,一次获取类别列表,再次找到丢失的列表。

SQL Fiddle

MySQL 5.6架构设置

CREATE TABLE IF NOT EXISTS `code` (
    `id`            INT(11) UNSIGNED    NOT NULL    AUTO_INCREMENT,
    `name`          VARCHAR(50)         NULL        DEFAULT NULL,
    PRIMARY KEY (`id`)
) 
    ENGINE=MyISAM 
    AUTO_INCREMENT=1 
    DEFAULT CHARSET=utf8 
    COLLATE=utf8_unicode_ci
    COMMENT '';

CREATE TABLE IF NOT EXISTS `code_category` (
    `id`            INT(11) UNSIGNED    NOT NULL    AUTO_INCREMENT,
    `codeid`        INT(11) UNSIGNED    NULL        DEFAULT NULL,
    `categoryid`    INT(11) UNSIGNED    NULL        DEFAULT NULL,
    `discount`      INT(11) UNSIGNED    NULL        DEFAULT NULL,
    PRIMARY KEY (`id`),
    KEY `idx_codecategory_catid` (`categoryid`),
    KEY `idx_codecategory_codeid` (`codeid`)
) 
    ENGINE=MyISAM 
    AUTO_INCREMENT=1 
    DEFAULT CHARSET=utf8 
    COLLATE=utf8_unicode_ci
    COMMENT '';

INSERT INTO `code`
(`id`,`name`)
VALUES
(1,'code1'),
(2,'code2'),
(3,'code3'),
(4,'code4');

INSERT INTO `code_category`
(`codeid`,`categoryid`,`discount`)
VALUES
(1,1,3),
(2,1,2),
(1,2,2),
(1,3,3),
(1,4,2),
(1,5,2),
(1,6,2),
(1,7,2),
(1,8,2),
(1,9,2),
(1,10,2),
(1,11,2),
(1,12,2),
(1,13,2),
(1,14,2),
(1,15,2),
(1,16,2),
(1,17,2),
(1,18,2),
(1,19,2),
(1,20,2);

查询1

SELECT 
        c.id,
        cc1.codeid,
        cc1.categoryid, 
        cc1.discount
    FROM code c
    LEFT JOIN code_category cc1
        ON cc1.codeid = 1
    LEFT JOIN code_category cc
        ON cc.codeid = c.id AND cc.codeid = cc1.codeid
    WHERE cc.codeid is null
  ORDER BY c.id,cc1.codeid,cc1.categoryid

<强> Results

| id | codeid | categoryid | discount |
|----|--------|------------|----------|
|  2 |      1 |          1 |        3 |
|  2 |      1 |          2 |        2 |
|  2 |      1 |          3 |        3 |
|  2 |      1 |          4 |        2 |
|  2 |      1 |          5 |        2 |
|  2 |      1 |          6 |        2 |
|  2 |      1 |          7 |        2 |
|  2 |      1 |          8 |        2 |
|  2 |      1 |          9 |        2 |
|  2 |      1 |         10 |        2 |
|  2 |      1 |         11 |        2 |
|  2 |      1 |         12 |        2 |
|  2 |      1 |         13 |        2 |
|  2 |      1 |         14 |        2 |
|  2 |      1 |         15 |        2 |
|  2 |      1 |         16 |        2 |
|  2 |      1 |         17 |        2 |
|  2 |      1 |         18 |        2 |
|  2 |      1 |         19 |        2 |
|  2 |      1 |         20 |        2 |
|  3 |      1 |          1 |        3 |
|  3 |      1 |          2 |        2 |
|  3 |      1 |          3 |        3 |
|  3 |      1 |          4 |        2 |
|  3 |      1 |          5 |        2 |
|  3 |      1 |          6 |        2 |
|  3 |      1 |          7 |        2 |
|  3 |      1 |          8 |        2 |
|  3 |      1 |          9 |        2 |
|  3 |      1 |         10 |        2 |
|  3 |      1 |         11 |        2 |
|  3 |      1 |         12 |        2 |
|  3 |      1 |         13 |        2 |
|  3 |      1 |         14 |        2 |
|  3 |      1 |         15 |        2 |
|  3 |      1 |         16 |        2 |
|  3 |      1 |         17 |        2 |
|  3 |      1 |         18 |        2 |
|  3 |      1 |         19 |        2 |
|  3 |      1 |         20 |        2 |
|  4 |      1 |          1 |        3 |
|  4 |      1 |          2 |        2 |
|  4 |      1 |          3 |        3 |
|  4 |      1 |          4 |        2 |
|  4 |      1 |          5 |        2 |
|  4 |      1 |          6 |        2 |
|  4 |      1 |          7 |        2 |
|  4 |      1 |          8 |        2 |
|  4 |      1 |          9 |        2 |
|  4 |      1 |         10 |        2 |
|  4 |      1 |         11 |        2 |
|  4 |      1 |         12 |        2 |
|  4 |      1 |         13 |        2 |
|  4 |      1 |         14 |        2 |
|  4 |      1 |         15 |        2 |
|  4 |      1 |         16 |        2 |
|  4 |      1 |         17 |        2 |
|  4 |      1 |         18 |        2 |
|  4 |      1 |         19 |        2 |
|  4 |      1 |         20 |        2 |
|----|--------|------------|----------|

您可能需要更改列名以匹配表。