I have a table with an existing user_agent
column that I want to make a new column based on if user agent contains the substrings 'windows'
or 'os x'
in MySQL and I can’t figure out what is wrong or find examples of case statements with LIKE
for strings on this site. My version of MySQL
is 8.0.26
.
SELECT user_id, CASE user_agent WHEN LIKE '%windows%' THEN 'Windows' WHEN LIKE '%OS X%' THEN 'Mac' Else 'Other' END AS Operating System FROM pageviews;
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
SELECT user_id, CASE WHEN user_agent LIKE '%windows%' THEN 'Windows' WHEN user_agent LIKE '%OS X%' THEN 'Mac' Else 'Other' END AS `Operating System` FROM pageviews;
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0