Running an "ORDER BY CASE" query on MySQL view breaks it afterwards Whenever I create a MySQL view and then run a query ordering the results with ORDER BY CASE WHEN (...), the view becomes unusable.

The newly created view works properly and all SQL is correct.

This is the error message I get when I try to view all records from the view (or when I click on "Browse" in phpMyAdmin):

#1064 - 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 'THEN0 ELSE1 END LIMIT 0, 30' at line 1


Here is example schema:

CREATE TABLE `comments` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `user_id` int(11) NOT NULL,
  `title` varchar(256) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=5 ;
INSERT INTO `comments` (`id`, `user_id`, `title`) VALUES
(1, 1, 'user one comment 1'),
(2, 1, 'user one comment 2'),
(3, 2, 'user two comment 1'),
(4, 2, 'user two comment 2');

CREATE TABLE `users` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `name` varchar(256) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=3 ;
INSERT INTO `users` (`id`, `name`) VALUES
(1, 'user1'),
(2, 'user2');


Sample view:

CREATE VIEW view_sample AS
SELECT
  c.id AS comment_id,
  c.title AS comment_title,
  u.name AS user_name
FROM comments c, users u
WHERE c.user_id = u.id


At this point you can verify that the view above works properly and shows results.

Sample query that will break everything:

SELECT * FROM view_sample
WHERE comment_title LIKE 'user one%'
ORDER BY CASE WHEN comment_title LIKE 'user one%' THEN 1 ELSE 0 END, comment_title


Query above will work, but whatever you try to do afterwards will fail. Try simply this:

SELECT * FROM view_sample


I tried this several times, with different views.

What am I doing wrong?
1)Syntax error: need add space between THEN and 0 (eq. ELSE).