SQL / PHP将字符串转换为float或int

我有这个SQL查询.. </ p>

 选择i。  invoiceid as transactionid,i.date,i.total,
'invoice'as transaction_type
from invoice
</ code> </ pre>

我在php中运行它并回显总数 在这样的foreach循环中.... </ p>

  echo gettype($ row ['total'])。  “&lt; br /&gt;”; 
</ code> </ pre>

并返回所有总计 string </ code> </ p>

如何将我的sql查询中的i.total转换为int或float?</ p>

我试过</ p>

convert(int, i.total)</ code> </ p>

并且没有用:(</ p>
</ div>

展开原文

原文

I have this sql query..

select i.invoiceid as transactionid, i.date, i.total, 
'invoice' as transaction_type
from invoice

and I ran it in php and echo out the total in a foreach loop like so....

echo gettype($row['total']) . "<br/>";

and all the totals returned string

how would I convert the i.total in my sql query to int or float?

i've tried

convert(int, i.total)

and that didnt work :(

2个回答



php将所有内容(不是数组/字典)作为字符串</ strong>处理,除非您明确告诉它不要 与(int)$ row ['total'] </ code>类似。 </ p>

http://php.net/manual/en/language.types.string.php#language.types.string.conversion </ p>
</ div>

展开原文

原文

php handles everything (that's not an array/dictionary) as a string unless you specifically tell it not to with something like (int)$row['total'].

http://php.net/manual/en/language.types.string.php#language.types.string.conversion



您可以尝试</ p>

 选择i.invoiceid作为transactionid,i.date,  CAST(i.total as int)为total,
'invoice'为transaction_type
from invoice
</ code> </ pre>
</ div>

展开原文

原文

You could try

select i.invoiceid as transactionid, i.date, CAST(i.total as int) as total, 
'invoice' as transaction_type
from invoice

dtxf759200
dtxf759200 这给了我一个SQL错误(1064)
8 年多之前 回复
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐