创建包含生成的列的表

时间:2019-04-17 13:32:22

标签: mysql sql

我们尝试用生成的列创建一个表。

请查看以下查询以了解我们当前的表结构:

表偏瘫:

CREATE TABLE `Heepsy` (
  `id` int(11) unsigned NOT NULL AUTO_INCREMENT,
  `username` varchar(200) COLLATE utf8mb4_bin DEFAULT NULL,
  `photo_url` varchar(300) COLLATE utf8mb4_bin DEFAULT NULL,
  `followers` int(10) DEFAULT NULL,
  `engagement` decimal(5,2) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=51731 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin;

Table HypeAuditor:

CREATE TABLE `HypeAuditor` (
  `id` int(11) unsigned NOT NULL AUTO_INCREMENT,
  `username` varchar(200) CHARACTER SET utf8mb4 COLLATE utf8mb4_bin DEFAULT NULL,
  `engaement` decimal(5,2) DEFAULT NULL,
  `Country` varchar(200) CHARACTER SET utf8mb4 COLLATE utf8mb4_bin DEFAULT NULL,
  `Country_percentage` int(3) DEFAULT NULL,
  `Interest1` varchar(200) CHARACTER SET utf8mb4 COLLATE utf8mb4_bin DEFAULT NULL,
  `Interest1_percentage` int(3) DEFAULT NULL,
  `Interest2` varchar(200) CHARACTER SET utf8mb4 COLLATE utf8mb4_bin DEFAULT NULL,
  `Interest2_percentage` int(3) DEFAULT NULL,
  `Interest3` varchar(200) CHARACTER SET utf8mb4 COLLATE utf8mb4_bin DEFAULT NULL,
  `Interest3_percentage` int(3) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=27123 DEFAULT CHARSET=utf8;

忍者桌外传:

CREATE TABLE `NinjaOutreach` (
  `id` int(11) unsigned NOT NULL AUTO_INCREMENT,
  `username` varchar(200) COLLATE utf8mb4_bin DEFAULT NULL,
  `fullname` varchar(200) COLLATE utf8mb4_bin DEFAULT NULL,
  `photo_url` varchar(300) CHARACTER SET utf8 DEFAULT NULL,
  `followers` int(10) DEFAULT NULL,
  `engagement` decimal(5,2) DEFAULT NULL,
  `city` varchar(100) CHARACTER SET utf8 DEFAULT NULL,
  `state` varchar(100) CHARACTER SET utf8 DEFAULT NULL,
  `country` varchar(100) CHARACTER SET utf8 DEFAULT NULL,
  `category_1` varchar(200) CHARACTER SET utf8 DEFAULT NULL,
  `category_2` varchar(200) CHARACTER SET utf8 DEFAULT NULL,
  `category_3` varchar(200) CHARACTER SET utf8 DEFAULT NULL,
  `category_4` varchar(200) CHARACTER SET utf8 DEFAULT NULL,
  `category_5` varchar(200) CHARACTER SET utf8 DEFAULT NULL,
  `category_6` varchar(200) CHARACTER SET utf8 DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=296788 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin;

科比尼尔表:

CREATE TABLE `Kombiniert` (
  `id` int(11) unsigned NOT NULL AUTO_INCREMENT,
  `username` varchar(200) CHARACTER SET utf8mb4 COLLATE utf8mb4_bin DEFAULT NULL,
  `fullname` varchar(200) CHARACTER SET utf8mb4 COLLATE utf8mb4_bin DEFAULT NULL,
  `followers_heepsy` int(11) DEFAULT NULL,
  `followers_ninjaoutreach` int(11) DEFAULT NULL,
  `followers_average` int(11) DEFAULT NULL,
  `engagement_heepsy` decimal(5,4) DEFAULT NULL,
  `engagement_ninjaoutreach` decimal(5,4) DEFAULT NULL,
  `engagement_hypeauditor` decimal(5,4) DEFAULT NULL,
  `engagement_average` decimal(5,4) DEFAULT NULL,
  `city` varchar(200) CHARACTER SET utf8mb4 COLLATE utf8mb4_bin DEFAULT NULL,
  `state` varchar(200) CHARACTER SET utf8mb4 COLLATE utf8mb4_bin DEFAULT NULL,
  `country` varchar(200) CHARACTER SET utf8mb4 COLLATE utf8mb4_bin DEFAULT NULL,
  `category1` varchar(200) CHARACTER SET utf8mb4 COLLATE utf8mb4_bin DEFAULT NULL,
  `category2` varchar(200) CHARACTER SET utf8mb4 COLLATE utf8mb4_bin DEFAULT NULL,
  `category3` varchar(200) CHARACTER SET utf8mb4 COLLATE utf8mb4_bin DEFAULT NULL,
  `category4` varchar(200) CHARACTER SET utf8mb4 COLLATE utf8mb4_bin DEFAULT NULL,
  `category5` varchar(200) CHARACTER SET utf8mb4 COLLATE utf8mb4_bin DEFAULT NULL,
  `category6` varchar(200) CHARACTER SET utf8mb4 COLLATE utf8mb4_bin DEFAULT NULL,
  `follower_interest1` varchar(200) CHARACTER SET utf8mb4 COLLATE utf8mb4_bin DEFAULT NULL,
  `follower_interest1_share` decimal(5,4) DEFAULT NULL,
  `follower_interest2` varchar(200) CHARACTER SET utf8mb4 COLLATE utf8mb4_bin DEFAULT NULL,
  `follower_interest2_share` decimal(5,4) DEFAULT NULL,
  `follower_interest3` varchar(200) CHARACTER SET utf8mb4 COLLATE utf8mb4_bin DEFAULT NULL,
  `follower_interest3_share` decimal(5,4) DEFAULT NULL,
  `follower_country` varchar(200) CHARACTER SET utf8mb4 COLLATE utf8mb4_bin DEFAULT NULL,
  `follower_country_share` decimal(5,4) DEFAULT NULL,
  `follower_country_total` int(11) DEFAULT NULL,
  `follower_country_engaged` int(11) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
  1. 我们如何更新SQL查询以创建具有以下生成列的表Kombiniert

followers_average: “ followers_heepsy”和“ followers_ninjaoutreach”列的平均值,但前提是两者均大于0。如果只有一列大于0,则使用此列的值。

参与度平均值: “ engagement_heepsy”列和“ engagement_ninjaoutreach”列以及“ engagement_hypeauditor”列的平均值,但前提是所有列均大于0。如果只有两列都大于0,请使用这两列的平均值。如果只有一列大于0,请使用此列的值。

follower_country_total: followers_average * follower_country_share

follower_country_engaged: followers_country_total *参与度平均

所有生成的列都应该存储为非虚拟的吗?

  1. 我们如何创建一个INSERT查询,该查询将执行以下操作:

    对于username中的每个HypeAuditor,使用表Kombiniert中的以下数据创建一行:

    https://docs.google.com/spreadsheets/d/1qU0WaExjg8cCsA3cdDvKf3UqdLxqXKw_8QnDRLvJwMA/edit?usp=sharing吗?

3 个答案:

答案 0 :(得分:0)

好的,我假设每个用户名上都有一个用户名联接和一个记录。

    INSERT INTO Kombiniert
(username, fullname, followers_heepsy, followers_ninjaoutreach, followers_average, engagement_heepsy, engagement_ninjaoutreach, engagement_hypeauditor, engagement_average, city, state, country, category1, category2, category3, category4, category5, category6, follower_interest1, follower_interest1_share, follower_interest2, follower_interest2_share, follower_interest3, follower_interest3_share, follower_country, follower_country_share, follower_country_total, follower_country_engaged)
    Select bb.*, follower_country_total*engagement_average as follower_country_engaged from
        (
            Select aa.*, followers_average*follower_country_share as follower_country_total from 
            (
                select 
                h.username,
                nj.fullname,
                hy.followers as followers_heepsy,
                nj.followers as followers_ninjaoutreach,
                case when (hy.followers=0 or hy.followers is null) and nj.followers>0 then nj.followers
                when hy.followers>0 and (nj.followers=0 or nj.followers is null) then hy.followers
                else (hy.followers+nj.followers)/2 end as followers_average,
                hy.engagement/100 as engagement_heepsy,
                nj.engagement/100 as engagement_ninjaoutreach,
                h.engagement/100 as engagement_hypeauditor,
                case    when (hy.engagement/100=0 or hy.engagement is null) and (nj.engagement/100=0 or nj.engagement is null) and h.engagement/100>0 then h.engagement/100
                        when (hy.engagement/100=0 or hy.engagement is null) and nj.engagement/100>0 and (h.engagement/100=0 or h.engagement is null) then nj.engagement/100
                        when hy.engagement/100>0  and (nj.engagement/100=0  or nj.engagement is null) and (h.engagement/100=0 or h.engagement is null) then hy.engagement/100
                        when (hy.engagement/100=0 or hy.engagement is null) and nj.engagement/100>0 and h.engagement/100>0 then (h.engagement/100+nj.engagement/100)/2
                        when hy.engagement/100>0  and nj.engagement/100>0 and (h.engagement/100=0 or h.engagement is null) then (hy.engagement/100+nj.engagement/100)/2
                        when hy.engagement/100>0  and (nj.engagement/100=0 or nj.engagement is null) and h.engagement/100>0 then (h.engagement/100+nj.engagement/100)/2
                        else (hy.engagement/100+nj.engagement/100+h.engagement/100)/3 end as engagement_average,
                nj.city,
                nj.state,
                nj.country ,
                nj.category_1,
                nj.category_2,
                nj.category_3,
                nj.category_4,
                nj.category_5,
                nj.category_6,
                h.interest1,
                h.Interest1_percentage/100 as follower_interest_share1,
                h.interest2,
                h.Interest2_percentage/100 as follower_interest_share2,
                h.interest3,
                h.Interest3_percentage/100 as follower_interest_share3,
                h.country as follower_country,
                h.country_percentage/100 as follower_country_share


                from HypeAuditor h
                left join Heepsy hy on h.username = hy.username
                left join NinjaOutreach nj on nj.username=h.username
                ) aa 
            ) bb

答案 1 :(得分:0)

我已经处理了@ianic的答案,并得出了以下结果:

INSERT INTO Kombiniert
(username, fullname, followers_heepsy, followers_ninjaoutreach, followers_average, engagement_heepsy, engagement_ninjaoutreach, engagement_hypeauditor, engagement_average, city, state, country, category1, category2, category3, category4, category5, category6, follower_interest1, follower_interest1_share, follower_interest2, follower_interest2_share, follower_interest3, follower_interest3_share, follower_country, follower_country_share, follower_country_total, follower_country_engaged)
    Select bb.*, follower_country_total*engagement_average as follower_country_engaged from
        (
            Select aa.*, followers_average*follower_country_share as follower_country_total from 
            (
                select 
                h.username,
                nj.fullname,
                hy.followers as followers_heepsy,
                nj.followers as followers_ninjaoutreach,
                case when hy.followers=0 and nj.followers>0 then nj.followers
                when hy.followers>0 and nj.followers=0 then hy.followers
                else (hy.followers+nj.followers)/2 end as followers_average,
                hy.engagement/100 as engagement_heepsy,
                nj.engagement/100 as engagement_ninjaoutreach,
                h.engagement/100 as engagement_hypeauditor,
                case    when hy.engagement/100=0 and nj.engagement/100=0 and h.engagement/100>0 then h.engagement/100
                        when hy.engagement/100=0 and nj.engagement/100>0 and h.engagement/100=0 then nj.engagement/100
                        when hy.engagement/100>0 and nj.engagement/100=0 and h.engagement/100=0 then hy.engagement/100
                        when hy.engagement/100=0 and nj.engagement/100>0 and h.engagement/100>0 then (h.engagement/100+nj.engagement/100)/2
                        when hy.engagement/100>0 and nj.engagement/100>0 and h.engagement/100=0 then (hy.engagement/100+nj.engagement/100)/2
                        when hy.engagement/100>0 and nj.engagement/100=0 and h.engagement/100>0 then (h.engagement/100+nj.engagement/100)/2
                        else (hy.engagement/100+nj.engagement/100+h.engagement/100)/3 end as engagement_average,
                nj.city,
                nj.state,
                nj.country ,
                nj.category_1,
                nj.category_2,
                nj.category_3,
                nj.category_4,
                nj.category_5,
                nj.category_6,
                h.interest1,
                h.Interest1_percentage/100 as follower_interest_share1,
                h.interest2,
                h.Interest2_percentage/100 as follower_interest_share2,
                h.interest3,
                h.Interest3_percentage/100 as follower_interest_share3,
                h.country as follower_country,
                h.country_percentage/100 as follower_country_share


                from HypeAuditor h
                left join Heepsy hy on h.username = hy.username
                left join NinjaOutreach nj on nj.username=h.username
                ) aa 
            ) bb

到目前为止,它工作正常,除了以下几点: HypeAuditor中有用户名,仅在表HeepsyNinjaOutreach中存在。如果是这种情况,则可以在此特定表的列中插入NULL,到目前为止还可以,但是两种计算平均值的情况不起作用。

如何修改案例以使其在这种情况下也能正常工作?

答案 2 :(得分:0)

只需借助@ianic很棒的查询找到最终答案!非常感谢!

这里是:

INSERT INTO Kombiniert
(username, fullname, followers_heepsy, followers_ninjaoutreach, followers_average, engagement_heepsy, engagement_ninjaoutreach, engagement_hypeauditor, engagement_average, city, state, country, category1, category2, category3, category4, category5, category6, follower_interest1, follower_interest1_share, follower_interest2, follower_interest2_share, follower_interest3, follower_interest3_share, follower_country, follower_country_share, follower_country_total, follower_country_engaged)
    Select bb.*, follower_country_total*engagement_average as follower_country_engaged from
        (
            Select aa.*, followers_average*follower_country_share as follower_country_total from 
            (
                select 
                h.username,
                nj.fullname,
                hy.followers as followers_heepsy,
                nj.followers as followers_ninjaoutreach,
                case when hy.followers IS NULL AND nj.followers>0 THEN nj.followers
                when hy.followers>0 AND nj.followers IS NULL THEN hy.followers
                else (hy.followers+nj.followers)/2 end as followers_average,
                hy.engagement/100 as engagement_heepsy,
                nj.engagement/100 as engagement_ninjaoutreach,
                h.engagement/100 as engagement_hypeauditor,
                case    when hy.engagement IS NULL AND nj.engagement IS NULL AND h.engagement>0 THEN h.engagement/100
                        when hy.engagement IS NULL AND nj.engagement>0 AND h.engagement IS NULL THEN nj.engagement/100
                        when hy.engagement>0 AND nj.engagement IS NULL AND h.engagement IS NULL THEN hy.engagement/100
                        when hy.engagement IS NULL AND nj.engagement>0 AND h.engagement>0 THEN (h.engagement/100+nj.engagement/100)/2
                        when hy.engagement>0 AND nj.engagement>0 AND h.engagement IS NULL THEN (hy.engagement/100+nj.engagement/100)/2
                        when hy.engagement>0 AND nj.engagement IS NULL AND h.engagement>0 THEN (h.engagement/100+hy.engagement/100)/2
                        else (hy.engagement/100+nj.engagement/100+h.engagement/100)/3 end as engagement_average,
                nj.city,
                nj.state,
                nj.country ,
                nj.category_1,
                nj.category_2,
                nj.category_3,
                nj.category_4,
                nj.category_5,
                nj.category_6,
                h.interest1,
                h.Interest1_percentage/100 as follower_interest_share1,
                h.interest2,
                h.Interest2_percentage/100 as follower_interest_share2,
                h.interest3,
                h.Interest3_percentage/100 as follower_interest_share3,
                h.country as follower_country,
                h.country_percentage/100 as follower_country_share


                from HypeAuditor h
                left join Heepsy hy on h.username = hy.username
                left join NinjaOutreach nj on nj.username=h.username
                ) aa 
            ) bb