为什么我不能在SQL中创建视图表?

时间:2017-05-13 10:20:43

标签: sql mariadb

我有这样的查询:

SELECT 
       generatedTime AS generatedTime,
       tempTable AS tempTable,
       CASE
           WHEN ((generatedTime < maxPelamarmhsdoktor) or (generatedTime < maxAkdmstmayor)
                    or (generatedTime < maxIpbmstdepartemen) or (generatedTime < maxIpbmstfakultas)
                        or (generatedTime < maxIpbrefjeniskelamin) or (generatedTime < maxJenisinstansi)
                            or (generatedTime < maxJenisinstitusi)) THEN
               'Yes'
           ELSE
               'No'
       END AS NeedsToUpdateorNot
FROM generatedtime
JOIN ( SELECT 
              MAX( last_update ) AS maxPelamarmhsdoktor
       FROM pelamarmhsdoktor

     ) AS maxPelamarmhsdoktorFinder
JOIN ( SELECT 
              MAX( last_update ) AS maxAkdmstmayor
       FROM akdmst_mayor

     ) AS maxAkdmstmayorFinder
JOIN ( SELECT 
              MAX( last_update ) AS maxIpbmstdepartemen
       FROM ipbmst_departemen

     ) AS maxIpbmstdepartemenFinder
JOIN ( SELECT 
              MAX( last_update ) AS maxIpbmstfakultas
       FROM ipbmst_fakultas

     ) AS maxIpbmstfakultasFinder
JOIN ( SELECT 
              MAX( last_update ) AS maxIpbrefjeniskelamin
       FROM ipbref_jeniskelamin

     ) AS maxIpbrefjeniskelaminFinder
JOIN ( SELECT 
              MAX( last_update ) AS maxJenisinstansi
       FROM jenisinstansi

     ) AS maxJenisinstansiFinder
JOIN ( SELECT 
              MAX( last_update ) AS maxJenisinstitusi
       FROM jenisinstitusi

     ) AS maxJenisinstitusiFinder

然后我想让该查询成为一个视图表。所以我添加了

CREATE ALGORITHM=UNDEFINED DEFINER=`root`@`localhost` SQL SECURITY DEFINER VIEW `updateforpelamarmagisterrinci`  AS 

一开始。

但是当我尝试创建视图时,出现错误,错误消息就像这样

#1349 - View's SELECT contains a subquery in the FROM clause

我可以做些什么来解决这个问题?提前致谢

4 个答案:

答案 0 :(得分:2)

由于您使用的是MariaDB 10.1,因此您可能需要考虑升级到MariaDB 10.2。在视图was removed in 10.2.1中使用子查询的限制。截至目前(2017年5月13日),最新的10.2版本为10.2.5-rc

下一个10.2.6将是GA(a.k.a.稳定版),即将推出。同时,您将有时间测试10.2功能是否适合您,这就是RC的用途。

注意:如果您决定尝试升级,强烈建议您在执行此操作之前创建数据库的备份。如果您因任何原因不喜欢该版本并希望降级,使用备份可能会省力。

答案 1 :(得分:1)

根据MySQL的documentation,您在创建FROM时无法在VIEW子句中进行子查询,因此会出错。但是,在您的情况下,您可以更改查询并尝试在SELECT子句中选择这些值,例如

SELECT 
       (SELECT MAX( last_update ) FROM pelamarmhsdoktor) AS maxPelamarmhsdoktorFinder,
       (SELECT MAX( last_update ) FROM akdmst_mayor) AS maxAkdmstmayorFinder,
       (SELECT MAX( last_update ) FROM ipbmst_departemen) AS maxIpbmstdepartemenFinder,
       (SELECT MAX( last_update ) FROM ipbmst_fakultas) AS maxIpbmstfakultasFinder,
       (SELECT MAX( last_update ) FROM ipbref_jeniskelamin) AS maxJeniskelamin,
       generatedTime AS generatedTime,
       tempTable AS tempTable,
       CASE
           WHEN ((generatedTime < maxPelamarmhsdoktor) or (generatedTime < maxAkdmstmayor)
                    or (generatedTime < maxIpbmstdepartemen) or (generatedTime < maxIpbmstfakultas)
                        or (generatedTime < maxIpbrefjeniskelamin) or (generatedTime < maxJenisinstansi)
                            or (generatedTime < maxJenisinstitusi)) THEN
               'Yes'
           ELSE
               'No'
       END AS NeedsToUpdateorNot
FROM generatedtime;

答案 2 :(得分:1)

这是一个解决方案,结构合理,但有些工作:

  1. 为需要时间列的每个表创建一个视图。您在此视图中所做的只是选择MAX(last_update)

  2. 创建一个视图,其中UNION在步骤1中创建的所有视图,然后再次选择MAX(last_update),为简单起见,我调用视图vAll

    < / LI>
  3. 现在您可以轻松创建您想要的视图:

    SELECT
       generatedTime AS generatedTime,
       tempTable AS tempTable,
       CASE WHEN generated_time < last_update THEN 'Yes' ELSE 'No' END AS NeedsToUpdateorNot
    FROM generatedtime, vAll;
    
  4. 或者尝试在select子句中执行所有操作:

    SELECT
       generatedTime AS generatedTime,
       tempTable AS tempTable,
       CASE WHEN generated_time < (
          SELECT MAX(last_update) AS last_update FROM (
              (SELECT MAX(last_update) AS last_update FROM a) UNION
              (SELECT MAX(last_update) AS last_update FROM b) UNION
              (SELECT MAX(last_update) AS last_update FROM c) UNION
              ...
          )
       ) THEN 'Yes' ELSE 'No' END AS NeedsToUpdateorNot
    FROM generatedtime;
    

答案 3 :(得分:0)

将您的子查询移至视图:

CREATE VIEW v_max_pelamarmhsdoktor AS 
    SELECT MAX( last_update ) AS maxPelamarmhsdoktor FROM pelamarmhsdoktor
...

...

您的查询变为:

SELECT 
       generatedTime AS generatedTime,
       tempTable AS tempTable,
       CASE
           WHEN ((generatedTime < maxPelamarmhsdoktor) or (generatedTime < maxAkdmstmayor)
                    or (generatedTime < maxIpbmstdepartemen) or (generatedTime < maxIpbmstfakultas)
                        or (generatedTime < maxIpbrefjeniskelamin) or (generatedTime < maxJenisinstansi)
                            or (generatedTime < maxJenisinstitusi)) THEN
               'Yes'
           ELSE
               'No'
       END AS NeedsToUpdateorNot
FROM generatedtime
JOIN v_max_pelamarmhsdoktor AS maxPelamarmhsdoktorFinder
...

...