开发者

Casting a CONCAT

Alrighty, I was tasked with getting the sale price for an item. I found to do this that I needed to take the data from the discount_percent and multiply it again开发者_开发技巧st card_price. (Don't really know how to multiply and subtract yet in mysql).

select discount_percent from card_sales order by card_id

The output of discount_percent is an integer (lets say for the sake of this, "50"). However, the discount_percent is a percentage and therefore needs a '.' in front of it. I therefore went use "CONCAT"

concat('.',card_sales.discount_percent) as 'newPrice'

This works great, but it is now not an integer. I then tried to cast it.

cast(concat('.',card_sales.discount_percent) as signed) as 'newPrice'

This apparently just throws up an error message. What am I doing wrong? Or is there an easier way to go about this?


If something has a decimal point in the first place, it clearly can't be an integer, yes? You'd want to cast it to DOUBLE, not SIGNED. But I'd say it's better to compute your whole expression numerically; you can get the percentage by dividing the value by 100, rather than by trying to construct the text of a decimal.

As far as multiplying,

SELECT 2 * 2;

returns "4". Here's some reference material on arithmetic in Mysql.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜