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

Time:05-24

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

CodePudding user response:

It sounds like you simply need:

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