Mysql CASE - QUANDO - allora - tornando tipo di dati errato (blob)
-
23-09-2019 - |
Domanda
Im creando prodotto personalizzabile attributi in negozio -. Ogni attributo può avere diversi tipi di dati, ciascun tipo di dati è memorizzato in una colonna separata utilizzando corrispondente tipo di dati mysql
Ho una domanda del tipo:
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 (...)
Ora, il problema è che quando attribute_types
.type
equivale a? Un certo tipo? Io voglio che restituisce un valore, come è memorizzato nella tabella product_attribute_values
.
Attualmente ho Blob ogni volta.
Dovrei usare tipo-casting o c'è qualche dietro le quinte magia che Non so circa, o forse c'è qualche alternativa migliore?
EDIT:
Tutto sembra essere a posto (im Controllo di prezzo che è flottante) fino a quando ho aggiungere una condizione per il testo (textarea).
Nessuna soluzione corretta
Altri suggerimenti
Sembra che si sta utilizzando alcune query browser. Prova l'esecuzione di questo comando, tramite l' 'Putty'.
Inoltre, per ottenere l'output corretto anche nel browser di query, includere la funzione CAST nell'istruzione caso come questo.
CAST(
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 CHAR) as value