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

MySQL

MySQL get missing IDs from table

1
2
3
4
5
6
7
8
SELECT a.id+1 AS start, MIN(b.id) - 1 AS end
FROM testtable AS a, testtable AS b
WHERE a.id < b.id
GROUP BY a.id
HAVING start < MIN(b.id)

-- http://stackoverflow.com/questions/12325132/mysql-get-missing-ids-from-table
-- http://www.codediesel.com/mysql/sequence-gaps-in-mysql/
https://snipt.net/embed/21272b09b0b86923c0a2890e893b1d9a/
/raw/21272b09b0b86923c0a2890e893b1d9a/
21272b09b0b86923c0a2890e893b1d9a
mysql
MySQL
8
2019-08-17T08:37:34
True
False
False
Nov 20, 2013 at 11:49 AM
/api/public/snipt/95552/
mysql-get-missing-ids-from-table
<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> <a href="#L-6">6</a> <a href="#L-7">7</a> <a href="#L-8">8</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">SELECT</span> <span class="n">a</span><span class="p">.</span><span class="n">id</span><span class="o">+</span><span class="mi">1</span> <span class="k">AS</span> <span class="n">start</span><span class="p">,</span> <span class="nf">MIN</span><span class="p">(</span><span class="n">b</span><span class="p">.</span><span class="n">id</span><span class="p">)</span> <span class="o">-</span> <span class="mi">1</span> <span class="k">AS</span> <span class="n">end</span> </span><span id="L-2"><a name="L-2"></a><span class="k">FROM</span> <span class="n">testtable</span> <span class="k">AS</span> <span class="n">a</span><span class="p">,</span> <span class="n">testtable</span> <span class="k">AS</span> <span class="n">b</span> </span><span id="L-3"><a name="L-3"></a><span class="k">WHERE</span> <span class="n">a</span><span class="p">.</span><span class="n">id</span> <span class="o">&lt;</span> <span class="n">b</span><span class="p">.</span><span class="n">id</span> </span><span id="L-4"><a name="L-4"></a><span class="k">GROUP</span> <span class="k">BY</span> <span class="n">a</span><span class="p">.</span><span class="n">id</span> </span><span id="L-5"><a name="L-5"></a><span class="k">HAVING</span> <span class="n">start</span> <span class="o">&lt;</span> <span class="nf">MIN</span><span class="p">(</span><span class="n">b</span><span class="p">.</span><span class="n">id</span><span class="p">)</span> </span><span id="L-6"><a name="L-6"></a> </span><span id="L-7"><a name="L-7"></a><span class="c1">-- http://stackoverflow.com/questions/12325132/mysql-get-missing-ids-from-table</span> </span><span id="L-8"><a name="L-8"></a><span class="c1">-- http://www.codediesel.com/mysql/sequence-gaps-in-mysql/</span> </span></pre></div> </td></tr></table>
mysql

Text only

MySQL join

http://twitpic.com/cgyaqq
https://snipt.net/embed/d695fa1a523dc4ea13d76e6947a316e6/
/raw/d695fa1a523dc4ea13d76e6947a316e6/
d695fa1a523dc4ea13d76e6947a316e6
text
Text only
1
2019-08-14T05:38:15
True
False
False
Apr 04, 2013 at 12:23 PM
/api/public/snipt/58797/
mysql-join
<table class="highlighttable"><tr><td class="linenos"><div class="linenodiv"><pre><a href="#L-1">1</a></pre></div></td><td class="code"><div class="highlight"><pre><span></span><span id="L-1"><a name="L-1"></a>http://twitpic.com/cgyaqq </span></pre></div> </td></tr></table>
mysql