0

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

mycode is:

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())

what is the problem with this code?I am not familier with view

hjpotter92
  • 78,589
  • 36
  • 144
  • 183
bon
  • 29
  • 4

1 Answers1

2

MySQL does not allow subqueries in the from clause when you define views.

You can do what you want as:

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();

My guess is that you actually want to change the ctype values as well. Otherwise the query is rather strange.

Here are the restrictions on views.

Gordon Linoff
  • 1,242,037
  • 58
  • 646
  • 786