我的查询语句中的IF()子句有问题。我只是将一个整数移到我在查询语句中动态创建的另一个列,但是这个整数丢失了它的属性分类,并且MySQL将其视为字符串。
mysql_query('
SELECT id, statusDate, displayName, earnings,
IF(statusDate <> "0000-00-00 00:00:00", earnings, "0") as earnings1
FROM my_table
ORDER BY statusDate, earnings1 DESC, displayName
');表中有一些earnings,比如10、50、200等。
因为我做的是降序排序,所以我期望: 200 50 10
但我得到的是: 10,200,50
关于如何在迁移到earnings1时将earnings保留为整数,有什么想法吗
谢谢。
发布于 2013-06-29 03:17:17
如果IF函数中的两个参数都是number,那么就不会出现这个问题。因此,如果earnings是数字,请尝试对您的earnings1值执行以下操作:
IF(status <> "0000-00-00 00:00:00", earnings, 0) as earnings1
^ note no more quotes发布于 2013-06-29 03:12:21
这对你有用吗?
SELECT id, statusDate, displayName, earnings,
(CAST( IF(statusDate <> "0000-00-00 00:00:00", earnings, 0) AS SIGNED)) as earnings1发布于 2013-06-29 03:16:53
您可能希望使用CASE语句,更重要的是,删除0中的引号“以保留int数据类型。
SELECT id, statusDate, displayName, earnings,
CASE WHEN statusDate <> "0000-00-00 00:00:00"
THEN earnings
ELSE 0
END as earnings1
FROM my_table
ORDER BY statusDate, earnings1 DESC, displayNamehttps://stackoverflow.com/questions/17371917
复制相似问题