My question is this: I have a mysql query:
SELECT student_id, name, email FROM student WHERE student_id IN ('1', '2', '3') OR name LIKE '%Nick%' OR email LIKE '%gmail.com%'
How can I get the number of matching fields in a column returned by mysql Something like this:
ID NAME EMAIL MATCHED 1. 1 Nick nick@gmail.com 3 2. 5 Nick nick@yahoo.com 1 3. 2 David david@gmail.com 2
Thank!
It's ugly, but something like
SELECT student_id, name, email, IF(student_id IN (1,2,3), 1, 0) + IF(name LIKE '%Nick%', 1, 0) + IF(email LIKE '%gmail.com%', 1, 0) AS matched etc...
gotta do the trick.
This should work
SELECT student_id, name, email, ( CASE WHEN student_id IN ('1', '2', '3') THEN 1 END + CASE WHEN name LIKE '%Nick%' THEN 1 END + CASE WHEN email LIKE '%gmail.com%' THEN 1 END ) as matched FROM student WHERE student_id IN ('1', '2', '3') OR name LIKE '%Nick%' OR email LIKE '%gmail.com%'