[英]MySQL: return "0" & "1" rather than "false" & "true"
I need to return true
or false
rather than 1
& 0
, using following query:我需要使用以下查询返回
true
或false
而不是1
& 0
:
select if(u.id is null,false,true) status
from user u
limit 10
the above query returns status
with value 0 or 1 rather than true
and false
,上面的查询返回值为 0 或 1 的
status
,而不是true
和false
,
Is there any way to fix this?有没有什么办法解决这一问题?
If you want, you can return the values as strings :如果需要,您可以将值作为字符串返回:
SELECT IF(u.id IS NULL, 'false', 'true') as status
FROM user u
LIMIT 10
TRUE/FALSE is equivalent to 1/0. TRUE/FALSE 相当于 1/0。 It's just a matter of how your front end displays it.
这只是您的前端如何显示它的问题。
If you need to return the strings "true" and "false" (which I don't suggest - handle that in the display) then you'll have to account for that as well:如果您需要返回字符串“true”和“false”(我不建议这样做 - 在显示中处理),那么您还必须考虑到这一点:
IF(IF(u.id ISNULL,false,true) = 1, 'TRUE', 'FALSE')
MySQL has no boolean datatype, so you need to stick with 0 and 1 on the MySQL side: MySQL 没有布尔数据类型,因此您需要在 MySQL 端坚持使用 0 和 1:
select if(u.id is null, 0, 1) status_int
from user u
limit 10
If you prefer a boolean over 0/1 in PHP, you can cast it like this:如果你更喜欢 PHP 中的 0/1 布尔值,你可以这样转换:
$status = (bool) $status_int;
You are missing single quotes.您缺少单引号。
select if(u.id is null,'false','true') status
from user u
limit 10;
I just had a similar issue in lua. For some reason if the table column type is "tinyint" and you call 1 or 0 it will return as true and false.我刚刚在 lua 中遇到了类似的问题。出于某种原因,如果表列类型为“tinyint”并且您调用 1 或 0,它将返回 true 和 false。 If you change the column type to "int" it will call and return in lua as 1 and 0.
如果将列类型更改为“int”,它将在 lua 中调用并返回 1 和 0。
Old post but I hope this helps someone!旧帖子,但我希望这对某人有帮助!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.