posted 14 years ago
Your SQL could look something like this:Several notes:
The messageId field appears to be a number, however you compared it to strings in your query. That leads to implicit conversions and is potentially dangerous; always avoid these.The outgoing list of valid IDs is strange; some nubers were repeated. I've sorted it and put it into an IN list so that it would be more apparent.The WHERE clause limits the list of valid messageId's. Numbers that do not appear there will never make it into selected rows (eg. messageId will never equal to 1 in the CASE expression), it is therefore useless and possibly confusing to list them in the CASE expression conditions. If you remove them, the lists will be shorter and more manageable.When direction is 'incoming', the value of checkbox will be either 'true' or null, when direction is 'outgoing', the value of checkbox will be either 'false' or null. For other values of direction it will be always null. Is this the expected behaviour? It seems a bit strange to me.If your query is slow, it probably is not because of the CASE expression. Try replacing the computed value of checkbox field with a constant or null and re-run the statement. I'd expect the runtime to be practically equal. The problem could lie in the way tables are joined in your query, may be a missing index or something.