user6494551
user6494551

Reputation:

Multiple replace statement SQL?

I want to take a field, search for the occurrence of 1 out of 2 possible names: cross or memory. Depending on which one it is, replace it with its corresponding img.

My current code (that works) is as follows:

SELECT REPLACE(column_name,'cross','<img src=\"crossimg.png\" />') AS 'column_name'
FROM my_table;

I want it to be something like this:

SELECT REPLACE(column_name, if == 'cross' then img1, elseif == 'memory' then img2) AS 'column_name'
FROM my_table;

Upvotes: 0

Views: 315

Answers (2)

user6494551
user6494551

Reputation:

I figured it out, I ended up going with:

$sql = "SELECT REPLACE(REPLACE(column_name,'second','<img src=\"secondimg.png\" />'),'first','<img src=\"firstimg.png\" />') AS column_name from my_table;

Upvotes: 0

aquinas
aquinas

Reputation: 23796

You probably are looking for a case statement:

SELECT case when column_name = 'cross' then img1 
when column_name='memory' then img2
else column_name end as column_name from my_table;

Upvotes: 1

Related Questions