Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

mysql - cannot create view that contains union

Tags:

mysql

union

view

I have a mysql query that uses union to join multiple queries into one result set. the query works perfectly.

When I want to use the same query to create a view I then get an error message:

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'union select hulaminloadnumber2,deliveryid,drop1customer from localjhb where hul' at line 2

my query that works perfectly is:

select hulaminloadnumber1,deliveryid,drop1customer from localjhb where hulaminloadnumber1>0
union
select hulaminloadnumber2,deliveryid,drop1customer from localjhb where hulaminloadnumber2>0
union
select hulaminloadnumber3,deliveryid,drop1customer from localjhb where hulaminloadnumber3>0
union
select hulaminloadnumber4,deliveryid,drop1customer from localjhb where hulaminloadnumber4>0
union
select hulaminloadnumber5,deliveryid,drop1customer from localjhb where hulaminloadnumber5>0

query results

my query to create the view is:

create view View_LoadvsCustomer as (
select hulaminloadnumber1,deliveryid,drop1customer from localjhb where hulaminloadnumber1>0
union
select hulaminloadnumber2,deliveryid,drop1customer from localjhb where hulaminloadnumber2>0
union
select hulaminloadnumber3,deliveryid,drop1customer from localjhb where hulaminloadnumber3>0
union
select hulaminloadnumber4,deliveryid,drop1customer from localjhb where hulaminloadnumber4>0
union
select hulaminloadnumber5,deliveryid,drop1customer from localjhb where hulaminloadnumber5>0)

this produces the below error in PHPMyadmin: error message

The queries are all from a badly designed table so there should be no formatting or collation issues. Can a view be created against a union query?

Any advice is appreciated.

Have a GREAT weekend, Ryan

like image 566
Smudger Avatar asked Mar 30 '12 10:03

Smudger


People also ask

How do I add a UNION to a view in MySQL?

The basic syntax for creating a view in MySQL is as follows: CREATE VIEW [db_name.] view_name [(column_list)] AS select-statement; [db_name.] is the name of the database where your view will be created; if not specified, the view will be created in the current database.

Is UNION supported in MySQL?

You can use UNION if you want to select rows one after the other from several tables or several sets of rows from a single table all as a single result set. UNION is available as of MySQL 4.0.

Can we create views in MySQL?

By default, a new view is created in the default database. To create the view explicitly in a given database, use db_name. view_name syntax to qualify the view name with the database name: CREATE VIEW test.


1 Answers

Remove the parenthesis from view definition. You have hit this server bug.

like image 182
Ashwin A Avatar answered Oct 02 '22 08:10

Ashwin A