包含子查询的MySQL temptable视图

时间:2014-08-04 12:13:30

标签: mysql sql temp-tables

我正在尝试使用子查询创建视图。 根据{{​​3}},当我的视图使用MERGE算法时,我无法执行此操作,但如果我使用temptable算法,则可以执行此操作。我正在做什么/读错了什么?

CREATE ALGORITHM =  temptable VIEW `vw_prod_placementinfo_destination` AS
select 
    d.branch,
    d.media_plan_name,  
    d.placement,
    case c.country WHEN null or 'n/a' then d.one else d.three end as Creative,
    case length(d.four)-length(replace(four, "x", '')) > 0 when true then d.four else Null end as AdSize
from (
    select 
        branch, 
        media_plan_name, 
        placement,
        split_str(placement, '_', 1) as One,
        split_str(placement, '_', 2) as Two,
        split_str(placement, '_', split_count(placement, '_')-1) as Three,
        split_str(placement, '_', split_count(placement, '_')) as Four
    from campaign_delivery_flat
    where media_plan_name like '%Destinatio%'
    group by branch, media_plan_name, placement ) d
left join country_code c on d.One = c.code

1 个答案:

答案 0 :(得分:1)

评论时间有点长。

您引用的文档在from子句中说明 nothing 有关子查询的内容。相应的文档是here。并且,它是相当明确和明确的:

  

子视图不能在视图的FROM子句中使用。

(第二段)。

如果需要进行设置,则为子查询创建一个视图,为外部查询创建一个视图。

或者,重写逻辑,这样您就不需要子查询。例如,您可以使用相关子查询来获取国家/地区:

select 
    branch, 
    media_plan_name, 
    placement,
    (case when exists (select 1 from country_code c on d.One = c.code)
          then split_str(placement, '_', 1)
          else split_str(placement, '_', split_count(placement, '_')-1)
     end) as creative
     as One,
     (case length( split_str(placement, '_', split_count(placement, '_')) )-length(replace( split_str(placement, '_', split_count(placement, '_')) , "x", '')) > 0
           when true then split_str(placement, '_', split_count(placement, '_'))
           else Null
      end) as AdSize
from campaign_delivery_flat
where media_plan_name like '%Destinatio%'
group by branch, media_plan_name, placement