XeWonder
XeWonder

Reputation: 39

mysql if exists return value otherwise retuen ZERo (or whatever to indentify)

I have a MySQL DB with one simple table...

id(key), messageid, time

I need a query that if i pass the messageid it will give me back the message id if it exists. otherwise any other value like 0.

I have tried "EXISTS" in various formulas but it either get 0 or 1. If it exists, I need the messageid for further processing.

Thank you all. DD

Upvotes: 0

Views: 226

Answers (1)

Stu
Stu

Reputation: 32579

It sounds like you simply need:

select case when exists 
  (select * from t where messageId = <value>) 
then <value> else 0 end as messageId;

Upvotes: 1

Related Questions