Mysql CASE - WHEN - THEN - 返回错误的数据类型(blob)
我在网上商店中创建可自定义的产品属性 - 每个属性可以有不同类型的数据,每个数据类型都使用相应的 mysql 数据类型存储在单独的列中。
我有这样的查询:
SELECT
products.id AS id,
products.sku AS sku,
products.name AS name,
products.url_key AS url_key,
attributes.name AS attribute,
CASE
WHEN `attribute_types`.`type` = 'text'
THEN product_attribute_values.value_text
WHEN `attribute_types`.`type` = 'float'
THEN product_attribute_values.value_float
WHEN `attribute_types`.`type` = 'price'
THEN product_attribute_values.value_float
WHEN `attribute_types`.`type` = 'integer'
THEN product_attribute_values.value_integer
WHEN `attribute_types`.`type` = 'multiple'
THEN product_attribute_values.value_text
WHEN `attribute_types`.`type` = 'dropdown'
THEN product_attribute_values.value_text
WHEN `attribute_types`.`type` = 'date'
THEN product_attribute_values.value_date
WHEN `attribute_types`.`type` = 'textarea'
THEN product_attribute_values.value_textarea
END as value
from (...)
现在,问题是当 attribute_types
.type
等于?some-type?我希望它返回一个存储在 product_attribute_values
表中的值。 目前我每次都会得到 BLOb。
我应该使用类型转换还是有一些我不知道的幕后魔法,或者也许有更好的选择?
编辑:
一切似乎都很好(我正在检查浮动价格),直到我添加文本(文本区域)的条件。
Im creating customizable product attributes in a web store - each attribute can have different type of data, each data type is stored in a separate column using corresponding mysql datatype.
I Have a query like:
SELECT
products.id AS id,
products.sku AS sku,
products.name AS name,
products.url_key AS url_key,
attributes.name AS attribute,
CASE
WHEN `attribute_types`.`type` = 'text'
THEN product_attribute_values.value_text
WHEN `attribute_types`.`type` = 'float'
THEN product_attribute_values.value_float
WHEN `attribute_types`.`type` = 'price'
THEN product_attribute_values.value_float
WHEN `attribute_types`.`type` = 'integer'
THEN product_attribute_values.value_integer
WHEN `attribute_types`.`type` = 'multiple'
THEN product_attribute_values.value_text
WHEN `attribute_types`.`type` = 'dropdown'
THEN product_attribute_values.value_text
WHEN `attribute_types`.`type` = 'date'
THEN product_attribute_values.value_date
WHEN `attribute_types`.`type` = 'textarea'
THEN product_attribute_values.value_textarea
END as value
from (...)
Now, the problem is that when attribute_types
.type
equals to ?some-type? i want it to return a value as it's stored in product_attribute_values
table.
Currently I get BLOb every time.
Should I use type-casting or there's some behind-the-scene magic that I dont know about, OR maybe there's some better alternative ?
EDIT:
Everything seems to be OKAY (im checking price that is float) until i add a condition for TEXT (textarea).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
看来您正在使用某种查询浏览器。尝试通过“Putty”执行此命令。
此外,为了即使在查询浏览器中也能获得正确的输出,请在 CASE 语句中包含 CAST 函数,如下所示。
It seems you are using some query browser. Try executing this command through 'Putty'.
Also, to get the correct output even in the query browser, include CAST function in your CASE statement like this.