MySQL Forums
Forum List  »  Newbie

Re: CASE and common table expression in mysql
Posted by: Peter Brawley
Date: December 30, 2019 10:11PM

Converting the query to conventional subqueries ...

select 
  user_name, rating, 
  CASE rating
    WHEN 0 THEN 'Really bad'
    WHEN 1 THEN 'Bad'
    WHEN 2 THEN 'Almost bad'
    WHEN 3 THEN 'So-so'
    WHEN 4 THEN 'OK'
    ELSE 'Loved it!'
  END AS rating_text
from (
  select 
    a.user_name, 
    (FLOOR(0 + RAND() * (5 - 0))) AS rating
  from (
    SELECT 'Asher' as user_name
    UNION ALL
    SELECT 'Cyd'
    UNION ALL
    SELECT 'Joe'
  ) a
) b;

manifests the same problem, I think Rand() causes the inconsistency---you'd need to move that random generation out of the query to make the ratings match.

Options: ReplyQuote


Subject
Written By
Posted
Re: CASE and common table expression in mysql
December 30, 2019 10:11PM


Sorry, you can't reply to this topic. It has been closed.

Content reproduced on this site is the property of the respective copyright holders. It is not reviewed in advance by Oracle and does not necessarily represent the opinion of Oracle or any other party.