在视图中选择列时,存储过程返回未知列

时间:2018-10-04 08:02:34

标签: php mysql stored-procedures

我有以下存储过程代码:

    SET @sql=NULL;
    SET SESSION group_concat_max_len = 1000000;

    Select group_concat( DISTINCT if(monthname(date_vis) is null, 
    CONCAT('max(if (monthname(date_vis) is null, state, 0)) as ''NULL'' '),
    CONCAT('max(if (monthname(date_vis)=''', monthname(date_vis), ''', week_total, "N/A")) 
as ''', monthname(date_vis), ''' ')) )
    into @sql from mon join (SELECT @sql:='')a WHERE agent_id = a1 ORDER BY MONTH(date_vis);
    set @sql = concat('SELECT CONCAT(doc.firstname," ",doc.lastname), ' , @sql , ' , 
    SUM(CASE When a.visit_status=1 Then 1 Else 0 End ) as total_p, 
    SUM(CASE When a.visit_status=0 OR a.visit_status="N/A" Then 1  Else 0 End ) as total_a 
    FROM  (visit a  INNER JOIN agent age on (a.agent_id = age.agent_id )
    JOIN doctor doc on(a.doc_id=doc.doc_id))  WHERE  a.agent_id = ', a1, ' 
    AND visit_status !=""
    GROUP BY a.doc_id 
    ORDER BY month(a.date_vis)');

    PREPARE stmt FROM @sql;
    EXECUTE stmt;
    DEALLOCATE PREPARE stmt;

它将返回错误消息,提示未知列'week_total'。 这是名为mon的视图表,其中包含上述列 ,其中week_total仅在mon 中)。

enter image description here

查询视图:

  CREATE VIEW `mon`  AS  select month(`visit`.`date_vis`) AS `w`,
sum(`visit`.`visit_status`) AS `week_total`,`visit`.`visit_status` AS `visit_status`,
`visit`.`date_vis` AS `date_vis`,
`visit`.`agent_id` AS `agent_id` 
from `visit` group by `visit`.`doc_id`,
month(`visit`.`date_vis`) ;

请帮助...

0 个答案:

没有答案