I am trying to achieve the following:
I have a single ORDER BY statement which could vary depending on the value stored in Column A.
For example:
if the Type is Member, sort by member last name
if the Type is Group, sort by the Group Name
both in Ascending order.
My best guess for the final statement would be:
SELECT *
FROM table
WHERE STATUS = 'Active'
ORDER BY ((LNAME if TYPE = 'Member') OR (GROUPNAME if TYPE = 'Group')) ASC
I know this is incorrect but cannot find information elsewhere. Any ideas?
Well, you can use the
IFfunction in MySQL (Note the emphasis onfunctionsince there’s also an unrelatedIFstatement)…:However, in this case it seems the better choice (From a flexibility standpoint) would be the
CASEstatement:Note that the entire block from
CASEtoENDis to be considered as a single “unit”. The result of which is what you’re trying to sort against (Hence why theASCcomes after the block, rather than inside of it)…