Note that there are some explanatory texts on larger screens.

plurals
  1. POMySQL Conditional Select Replace Query
    text
    copied!<p>I have found myself in need of replacing certain returned results from a MySQL Select Query. I have the following table and data (simplified for example purposes)</p> <pre><code>uid | duration | range | unique | stamp ----------------------------------------------------------------- 23 | d | 43 | 1 | 1 24 | d | 65 | 0 | 2 25 | d | 76 | 0 | 3 26 | d | 33 | 0 | 4 27 | d | 44 | 1 | 5 28 | d | 43 | 1 | 6 29 | d | 67 | 0 | 7 30 | d | 88 | 0 | 8 31 | d | 63 | 0 | 9 </code></pre> <p>The <code>stamp</code> column is what I want to do the replace on. Rather than a simple text replace, I was wondering if its possible to run some sort of user defined function on the column and replace it dynamically.</p> <p>For example If the data returned in the stamp column is a 1, I would like it to replace it with today's timestamp, if it is a 2 then yesterdays timestamp, a 3, the day before yesterdays and so on and so forth. </p> <p>So my question is, is it possible to point <code>REPLACE</code> to a function that processes the value and then returns what to replace it with. Or if not, is there another way to accomplish this. </p> <p>I could obviously post process the returned data in PHP and make the changes, but with millions of records returned, it will increase the load time considerably.</p> <p><strong>EDIT TO MAKE THINGS A BIT CLEARER:</strong> I want to replace the stamp column in the data returned from a SELECT Query, I am not storing the data anywhere, or replacing the data in the table. The table will remain unchanged.</p> <p>Thanks</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload