REEMPLAZAR y SI en la expresión provoca la concatenación de la fila anterior

The following sql statement:

SELECT 
    profile_pic
FROM
    (`member`) 
WHERE 
    `active` = 1

produce el siguiente resultado:

profile_pic
    1_1345694557.jpg
    <blank_value>
    <blank_value>
    <blank_value>

I want the "blank values" to default to "no_prof_thumb.jpg".

So I created this statement:

SELECT 
    REPLACE(IF (CHAR_LENGTH(profile_pic) > 0, profile_pic, 'no_prof.jpg' ), '.jpg', '_thumb.jpg') AS profile_pic 
FROM 
    (`member`) 
WHERE 
    `active` = 1

Aquí está el resultado:

profile_pic
1_1345694557_thumb.jpg
no_prof_thumb.jpg
no_prof_thumb_thumb.jpg
no_prof_thumb_thumb_thumb.jpg

Por que _thumb keep appending itself to the value in the previous row?

How should I fix my expression?


Noticias

This issue occurs on a GoDaddy mySQL database. I reproduce when it connecting remotely through SQLyog and when I log into GoDaddy and run it through phpMyAdmin.

However, I cannot reproduce locally nor on sqlFiddle.
So it must be some kind of configuration issue.

preguntado el 25 de agosto de 12 a las 04:08

2 Respuestas

Necesitabas usar IF afuera de REPLACE declaración como esta.

SELECT 
    IF (profile_pic = '', 'no_prof.jpg', 
        REPLACE(profile_pic, '.jpg', '_thumb.jpg')) AS profile_pic 
FROM 
    (`member`) 
WHERE 
    `active` = 1

También puedes usar CASE declaración en lugar de IF Me gusta esto:

SELECT
  CASE  profile_pic
    WHEN '' THEN 'no_prof.jpg'
    ELSE REPLACE(profile_pic, '.jpg', '_thumb.jpg')
  END AS profile_pic
FROM member
WHERE active = 1;

Mira este violín

Respondido 25 ago 12, 05:08

I reproduced this issue on a GoDaddy mysql database. I tested the sql on sqlfiddle and locally and could not reproduce. So, it must be some kind of configuration issue on their servers. Your suggestion works though. Ty. - rayo

The nested logic is a little weird. You can simplify it with a CASE statement. If the value is an empty string, just return no_prof_thumb.jpg. Otherwise, replace .jpg con _thumb.jpg.

SELECT
  CASE  
    WHEN profile_pic = '' THEN 'no_prof_thumb.jpg'
    ELSE REPLACE(profile_pic, '.jpg', '_thumb.jpg')
  END AS profile_pic
FROM member
WHERE active = 1

I can't, explain why you're getting the weird result you're getting without further testing though.

Respondido 25 ago 12, 14:08

small syntax issue: closing parenthesis on REPLACE. Otherwise, it works. - rayo

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.