简体   繁体   中英

What's the difference between CASE and IF in mysql

I found very strange MySQL behavior:

I have a string with umlaut letters, and I run some IF expression on it. Something like:

IF(length(field) < 10, '', replace(field, "\n", "<BR>"))

It it works fine.

However, if I replace this if by CASE, then the result is cut on the first unlaut letter!

CASE WHEN length(field)<10 THEN '' ELSE replace(field, "\n", "<BR>") END

Also, I noticed that it happens only when there is also GROUP BY part in the query.

I can't understand what's the difference between CASE and IF - from logical point of view both should return the same result exactly.

Anyone knows why the is difference between these two commands?

"IF is a single fork, "CASE" can be multiple Use "Case" if you have more than two values optional values, "IF" when you have only two values.

General structure of CASE is:

CASE x
WHEN a THEN ..
WHEN b THEN ..
...
ELSE
END

General structure of IF:

IF (expr)
THEN...
ELSE...
END

So, basically IF is a CASE with only one 'WHEN' statement.

好像有些mysql内部实现“功能”。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM