PHP mySQL count number of fields not empty
- by Pez Cuckow
I have a database of users where they can send messages to other users (up to four) and the ID of the message they sent is in their user row.
e.g.
Name, Email, Msg1, Msg2, Msg3, Msg4
Pez, [email protected], 1, 55, 42, 5 //Send 4 messages
Steve, [email protected], 0, 0, 0, 0 //Send 0 messages
Leon, [email protected], 3, 0, 3, 5 //Send 3 messages
How in a MySQL query can I get the amount of those message rows that are not empty or not equal to 0, allowing me to order by that? So it would return
Pez - 4 Mesasges
Leon - 3 Messages
Steve - 0 Messages
Im my mind something like order by count(!empty(msg1)+!empty(msg2)+!empty(msg3)+!empty(msg4))
Many thanks,