Mr. A Mr. A - 1 year ago 68
MySQL Question

How does the ORDER BY CASE Clause Work in the following Code?

How does this part of code work?

CASE WHEN subject IN ('Chemistry','Physics') THEN 1 ELSE 0 END,

Answer Source

First it checks if subjet is Chemistry or Physics. If it does the sorting score will be 1.

If the subject does not contain any of "Chemistry" or "Physics" the score will be 0.

After that it sorts the results by score so the results that contains Chemistry or Physics will come first after that those that dont contain.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download