Pergunta

Estou criando atributos de produto personalizáveis ​​em uma loja virtual - cada atributo pode ter diferentes tipos de dados, cada tipo de dados é armazenado em uma coluna separada usando o tipo de dados MySQL correspondente.

Eu tenho uma consulta como:

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 (...)

Agora, o problema é que quando attribute_types.type é igual a? algum tipo?eu quero que ele retorne um valor conforme está armazenado em product_attribute_values mesa.Atualmente recebo BLOb todas as vezes.

Devo usar a conversão de tipos ou há alguma mágica nos bastidores que eu não conheço, OU talvez haja alguma alternativa melhor?

EDITAR:

Tudo parece estar OK (estou verificando o preço flutuante) até adicionar uma condição para TEXT (textarea).

Nenhuma solução correta

Outras dicas

Parece que você está usando algum navegador de consulta.Tente executar este comando através do 'Putty'.

Além disso, para obter a saída correta mesmo no navegador de consulta, inclua a função CAST em sua instrução CASE como esta.

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
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top