创建视图时出错

时间:2014-07-30 12:00:15

标签: mysql

我收到错误#1349 - View's SELECT contains a subquery in the FROM clause

mycode是:

CREATE VIEW `MyViewName` AS
SELECT *
FROM
  (SELECT *
   FROM (
           (SELECT *
            FROM `crm_clients`
            WHERE ctype=1
            ORDER BY rand() LIMIT 0,
                                  3)
         UNION
           (SELECT *
            FROM `crm_clients`
            WHERE ctype=1
            ORDER BY rand() LIMIT 0,
                                  3)
         UNION
           (SELECT *
            FROM `crm_clients`
            WHERE ctype=1
            ORDER BY rand() LIMIT 0,
                                  3)
         UNION
           (SELECT *
            FROM `crm_clients`
            WHERE ctype=1
            ORDER BY rand() LIMIT 0,
                                  3)) t
   ORDER BY rand())

这段代码有什么问题?我对视图不是很熟悉

1 个答案:

答案 0 :(得分:2)

在定义视图时,MySQL不允许from子句中的子查询。

你可以做你想做的事:

Create View `MyViewName` as
    (SELECT * FROM `crm_clients` WHERE ctype=1 order by rand() limit 0,3)
    union 
    (SELECT * FROM `crm_clients` WHERE ctype=1 order by rand() limit 0,3)
    union
    (SELECT * FROM `crm_clients` WHERE ctype=1 order by rand() limit 0,3)
    union
    (SELECT * FROM `crm_clients` WHERE ctype=1 order by rand() limit 0,3)
    order by rand();

我的猜测是你实际上也希望更改ctype值。否则查询会很奇怪。

Here是对视图的限制。