I've got a table, that hasn't been normalised and we're not in a position to fix it anytime soon. I have a requirement that will be much easier if I create a view of that table with a union and make it look normalised. So I've got a couple of fiddles In this one I can run a query with the union and get the predicted response http://sqlfiddle.com/#!9/c4d829/1 In this one, though, I get errors if I put in the full union code (below) but it runs just fine as a single query but not with the union - on the line that says "union" I have an "invalid character" http://sqlfiddle.com/#!9/077e9b UNION SELECT registrations.`id` , registrations.`membership_id` , registrations.`partner_id` , registrations.`content_id` , registrations.`fees` , registrations.`options` , RPAD(registrations.`partnerattended`,3,' ') AS `attended` FROM `registrations` WHERE registrations.`softdelete` = 'N' AND registrations.`partner_id` IS NOT NULL Code (markup): Does anyone have any ideas what could be wrong? FWIW v5.5
That's this one - http://sqlfiddle.com/#!9/077e9b but I can't save it with the extra sql in it. The entire schema should be: create table `registrations` (`id` int(9) not null auto_increment, `membership_id` int(9), `individual_id` int(9), `partner_id` int(9) null, `content_id` int(9) null, `individualattended` char(3) null, `partnerattended` char(3) null, `fees` int(5), `options` text, `softdelete` char(1), primary key (`id`)); insert into `registrations` values (1, 43, 2, 3, 4000, 'Yes', 'Yes', 50, '','N') , (2, 43, 2, null, 4001, 'Yes', '', 150, '','N') , (3, 44, 4, null, 4001, 'Yes', '', 250, '','N') , (4, 45, 5, null, 4001, 'Yes', '', 350, '','N') , (5, 46, 6, 9, 4001, 'Yes', 'No', 450, '','N') , (6, 47, 7, 8, 4001, 'Yes', 'No', 550, '','N'); CREATE VIEW `vRegistrations` AS (SELECT registrations.`id` , registrations.`membership_id` , registrations.`individual_id` , registrations.`content_id` , registrations.`fees` , registrations.`options` , RPAD(registrations.`individualattended`,3,' ') AS `attended` FROM `registrations` WHERE registrations.`softdelete` = 'N' AND registrations.`individual_id` IS NOT NULL UNION SELECT registrations.`id` , registrations.`membership_id` , registrations.`partner_id` , registrations.`content_id` , registrations.`fees` , registrations.`options` , RPAD(registrations.`partnerattended`,3,' ') AS `attended` FROM `registrations` WHERE registrations.`softdelete` = 'N' AND registrations.`partner_id` IS NOT NULL ); Code (markup):
I don't, seriously, don't believe this. So, without the union the syntax with parentheses around the select statement works CREATE VIEW `vRegistrations` AS ( SELECT ...); Code (markup): However, when you have a union you have to take the parentheses off! I'm sure that makes sense to somebody, somewhere but it looks crazy to me. ref: https://stackoverflow.com/questions...reate-view-that-contains-union#answer-9941656