MySQL返回true / false如果col值!=指定的值 [英] MySQL return true/false if col value != specified value

查看:136
本文介绍了MySQL返回true / false如果col值!=指定的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



在我的实例中,我想要一个基于列的值来返回true / false(实际上是1/0)的MySQL查询。如果myDateTime的值为000-00-00 00:00:00我要返回hasDate()返回hasDate的日期时间col,我们称之为myDateTime



< (true)



如果myDateTime有任何其他值,我想返回hasDate为1

使用 IF 语句 p>

  SELECT IF(myDateTime ='0000-00-00 00:00:00',0,1)AS hasDate FROM myTable 

但最好的想法是更改数据结构,以避免这样做。


How can I write a MySQL query that returns true/false (1/0 actually) based upon the value of a column.

In my instance I want to return "hasDate" for a date time col, let's call it myDateTime

if myDateTime has a value of 000-00-00 00:00:00 I want to return hasDate of 0 (false)

If myDateTime has any other value I want to return hasDate of 1 (true)

Thanks for your assistance.

解决方案

Use a IF statement

SELECT IF(myDateTime = '0000-00-00 00:00:00',0,1) AS hasDate FROM myTable

But the best idea would be to change your data structure to avoid doing that.

这篇关于MySQL返回true / false如果col值!=指定的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆