Query return in which field is the value

Asked

Viewed 26 times

-1

I have a query in PHP + Mysql:

SELECT * From tb WHERE campo1='valor' or campo2='valor' or campo3='valor'

I would like the query to return to me in which of the fields was found the value.

1 answer

1


Use CASE / WHEN:

SELECT *, CASE WHEN campo1='valor' THEN 'campo1' WHEN campo2='valor' THEN 'campo2' ELSE 'campo3' END AS campo
FROM tb 
WHERE campo1='valor' or campo2='valor' or campo3='valor'

Browser other questions tagged

You are not signed in. Login or sign up in order to post.