PHP mySQL count number of fields not empty

Posted by Pez Cuckow on Stack Overflow See other posts from Stack Overflow or by Pez Cuckow
Published on 2010-03-25T11:02:02Z Indexed on 2010/03/25 11:13 UTC
Read the original article Hit count: 512

Filed under:
|

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,

© Stack Overflow or respective owner

Related posts about mysql

Related posts about php