snipt

Ctrl+h for KB shortcuts

MySQL

MYSQL insert random from list

1
2
3
4
5
INSERT INTO `im`
(`im`, `service`)
SELECT LOWER(`last`),
    ELT(0.5 + RAND() * 6, 'AIM', 'ICQ', 'MSN', 'Yahoo', 'GTalk', 'Other')
FROM `contact`
https://snipt.net/embed/8d58c72edaccf531a160855108a72482/
/raw/8d58c72edaccf531a160855108a72482/
8d58c72edaccf531a160855108a72482
mysql
MySQL
6
2019-08-19T09:56:47
True
False
False
Jan 09, 2014 at 04:42 PM
/api/public/snipt/104619/
mysql-insert-random-from-list
<table class="highlighttable"><tr><td class="linenos"><div class="linenodiv"><pre><a href="#L-1">1</a> <a href="#L-2">2</a> <a href="#L-3">3</a> <a href="#L-4">4</a> <a href="#L-5">5</a></pre></div></td><td class="code"><div class="highlight"><pre><span></span><span id="L-1"><a name="L-1"></a><span class="k">INSERT</span> <span class="k">INTO</span> <span class="ss">`im`</span> </span><span id="L-2"><a name="L-2"></a><span class="p">(</span><span class="ss">`im`</span><span class="p">,</span> <span class="ss">`service`</span><span class="p">)</span> </span><span id="L-3"><a name="L-3"></a><span class="k">SELECT</span> <span class="nf">LOWER</span><span class="p">(</span><span class="ss">`last`</span><span class="p">),</span> </span><span id="L-4"><a name="L-4"></a> <span class="nf">ELT</span><span class="p">(</span><span class="mi">0</span><span class="p">.</span><span class="mi">5</span> <span class="o">+</span> <span class="nf">RAND</span><span class="p">()</span> <span class="o">*</span> <span class="mi">6</span><span class="p">,</span> <span class="s1">&#39;AIM&#39;</span><span class="p">,</span> <span class="s1">&#39;ICQ&#39;</span><span class="p">,</span> <span class="s1">&#39;MSN&#39;</span><span class="p">,</span> <span class="s1">&#39;Yahoo&#39;</span><span class="p">,</span> <span class="s1">&#39;GTalk&#39;</span><span class="p">,</span> <span class="s1">&#39;Other&#39;</span><span class="p">)</span> </span><span id="L-5"><a name="L-5"></a><span class="k">FROM</span> <span class="ss">`contact`</span> </span></pre></div> </td></tr></table>
mysql