在MySQL中创建VIEW期间出现语法错误

时间:2017-11-27 07:26:43

标签: java mysql jboss

我正在尝试在MySQL中编写VIEW查询。以下是我的疑问。

CREATE VIEW SAMPLE_TEST(server, xml) AS
  WITH ServerSettings(server, ServerSettings) AS (
    SELECT
      S.ID
    FROM SERVER S
    INNER JOIN SAMPLE_MAC DMA ON S.DOMAIN = DMA.DOMAIN
    ORDER BY S.ID
  ),
  DomainSettings(server, DomainSettings) AS (
    SELECT
      S.ID,
    FROM SERVER S
    INNER JOIN DOMAIN D ON D.ID = S.DOMAIN
    ORDER BY S.ID
  )

  SELECT SS.server,
    XMLCONCAT(
      DS.DomainSettings,
      SS.ServerSettings
    )
  FROM ServerSettings SS
  INNER JOIN DomainSettings DS ON SS.server = DS.server;

当我在MySQL上运行时,我收到以下错误消息。

right syntax to use near 'ServerSettings(server, ServerSettings) AS (
    SELECT
      S.ID
    FROM SERVE' at line 6

但根据MySQL文档,我可以看到CREATE VIEW AS WITH已存在。我仍然缺少任何语法错误?有任何建议,请

1 个答案:

答案 0 :(得分:1)

  

那么如何在MySQL 5.7.12中编写这些查询?

使用子查询或创建多个视图。

1)子查询

 CREATE VIEW SAMPLE_TEST(server, xml) AS
  SELECT SS.server,
    XMLCONCAT(
      DS.DomainSettings,
      SS.ServerSettings
    )
  FROM (
         SELECT S.ID AS SERVER
              , S.SERVERSETTINGS -- ?
           FROM SERVER S
          INNER JOIN SAMPLE_MAC DMA 
             ON S.DOMAIN = DMA.DOMAIN 
        ) SS
        INNER JOIN (SELECT S.ID AS SERVER,
                         ,  S.DOMAINSETTINGS -- ?
                      FROM SERVER S
                           INNER JOIN DOMAIN D 
                              ON D.ID = S.DOMAIN) DS 
           ON SS.server = DS.server;

2)分开观点

CREATE VIEW ServerSettings AS (
    SELECT S.ID AS SERVER
         , S.SETTINGS   -- SETTING COLUMN(S)? 
      FROM SERVER S
           INNER JOIN SAMPLE_MAC DMA 
              ON S.DOMAIN = DMA.DOMAIN;

CREATE VIEW DomainSettingsAS AS (
    SELECT S.ID AS SERVER,
         , D.DOMAINSETTINGS -- DOMAIN SETTINGS COLUMN(S) 
      FROM SERVER S
             INNER JOIN DOMAIN D 
                ON D.ID = S.DOMAIN;

SELECT SS.server,
       XMLCONCAT(
             DS.DomainSettings,
             SS.ServerSettings )
  FROM ServerSettings SS
       INNER JOIN DomainSettings DS 
          ON SS.server = DS.server;