mySQL如果存在返回值,则否则请零(或任何归因)

发布于 2025-01-31 12:56:06 字数 193 浏览 3 评论 0原文

我有一个带有一个简单表的MySQL DB ...

ID(键),MessageID,时间

我需要一个查询,如果我传递MessageID,如果它存在,它将给我退还消息ID。否则,任何其他值,例如0。

我尝试以各种公式为“存在”,但它要么获得0或1。 如果存在,我需要MessageID进行进一步处理。

谢谢大家。 DD

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

猫烠⑼条掵仅有一顆心 2025-02-07 12:56:06

听起来就像您只需要:

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

It sounds like you simply need:

select case when exists 
  (select * from t where messageId = <value>) 
then <value> else 0 end as messageId;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文