<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Silveira Neto &#187; sql</title>
	<atom:link href="http://silveiraneto.net/tag/sql/feed/" rel="self" type="application/rss+xml" />
	<link>http://silveiraneto.net</link>
	<description>the world is a pixel</description>
	<lastBuildDate>Sun, 08 Jan 2012 05:17:57 +0000</lastBuildDate>
	<language>pt-br</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.5</generator>
		<item>
		<title>Short urls with Glassfish+MySQL</title>
		<link>http://silveiraneto.net/2008/10/21/short-urls-with-glassfishmysql/</link>
		<comments>http://silveiraneto.net/2008/10/21/short-urls-with-glassfishmysql/#comments</comments>
		<pubDate>Wed, 22 Oct 2008 02:27:18 +0000</pubDate>
		<dc:creator>Silveira</dc:creator>
				<category><![CDATA[english]]></category>
		<category><![CDATA[GlassFish]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[Linux]]></category>
		<category><![CDATA[mysql]]></category>
		<category><![CDATA[servlet]]></category>
		<category><![CDATA[sql]]></category>
		<category><![CDATA[tutorial]]></category>
		<category><![CDATA[URI]]></category>
		<category><![CDATA[url]]></category>
		<category><![CDATA[Web Application]]></category>
		<category><![CDATA[xort]]></category>

		<guid isPermaLink="false">http://silveiraneto.net/?p=1503</guid>
		<description><![CDATA[Pipes, Creative Commons photo by flattop341. 1. The Problem Internet is full of long urls and meaningless. Long urls are difficult to remember or print, usually full of redundancy and low semantic. With short and meaningful urls you can avoid thes problems and even achieve profitable goals with SEO SEO (search engine optimization) technics. There [...]]]></description>
			<content:encoded><![CDATA[<p style="text-align: center;"><img class="size-full wp-image-1517 aligncenter" title="pipes_by_flattop341_on_flickr" src="http://silveiraneto.net/wp-content/uploads/2008/10/pipes_by_flattop341_on_flickr.jpg" alt="" width="500" height="375" /><br />
Pipes, Creative Commons photo by <a href="http://flickr.com/photos/flattop341/253765659/">flattop341</a>.</p>
<h2>1. The Problem</h2>
<p>Internet is full of long urls and meaningless.</p>
<p>Long urls are difficult to remember or print, usually full of redundancy and low semantic. With short and meaningful urls you can avoid thes problems and even achieve profitable goals with SEO<br />
SEO (search engine optimization) technics.</p>
<p>There are services like <a title="Tiny URL" href="http://tinyurl.com/">Tiny URL</a>, <a title="Fancy URL" href="http://www.fancyurl.com/home.php">Fancy URL</a>, <a title="Moo URL" href="http://moourl.com/">Moo URL</a> and others. Although they solve part of the problems, they bring <a title="Wikipedia, english" href="http://en.wikipedia.org/wiki/TinyURL#Criticism">several others</a>. Another problem is if you have a web site like example.com and use a third-party service for short urls you are losing part of your mind-share with your users and clients.</p>
<p>As an example, if a example.com company wants to promote a open work position would be preferable spread a example.com/jobs instead of a tinyurl.com/examplejobs, or even worst, a tinyurl.com/3i4i592 (meaningless hash).</p>
<h2>2. Solution Approach</h2>
<p>I created a little program called <a title="xort project page" href="http://xort.dev.java.net">xort</a> that can be placed on your own server and provide you own short maintening your base url.</p>
<p style="text-align: center;"><img title="pipe_abstraction" src="http://silveiraneto.net/wp-content/uploads/2008/10/pipe_abstraction.png" alt="" /></p>
<p>I use a pipe abstraction. Each pipe redirects from a key url to an output url.</p>
<p>The idea is that you have <a title="xort project page" href="http://xort.dev.java.net">xort</a> installed and associated into your domain (preferably on /x). A pipe inside example.com would be like example.com/x/jobs.</p>
<h2>3. Tools</h2>
<p>All those tools are multi platform, open source and free.</p>
<h3>3.1 Glassfish Application Server</h3>
<p style="text-align: center;"><img title="Glassfish logo" src="../wp-content/uploads/2008/09/glassfish_logo_large.gif" alt="" /></p>
<p><a title="Glassfish Project" href="http://glassfish.dev.java.net">Glassfish</a> is an open source application server project led by Sun Microsystems for the Java Enterprise Edition (Java EE) platform. It&#8217;s very easy to install and run and have a very nice administration web interface where you can do from simple tasks like deploy a application to more complexes like clustering.</p>
<p style="text-align: center;"><img class="size-full wp-image-1249" title="glassfish_web_apps" src="../wp-content/uploads/2008/09/glassfish_web_apps.png" alt="" /><br />
Glassfish Admin Console</p>
<p>To develop the application I&#8217;m using <a title="NetBeans 6.5 Beta Download" href="http://download.netbeans.org/netbeans/6.5/beta/">NetBeans 6.5 Beta</a> that comes with <em>Glassfish V3 prelude b15b</em>. Netbeans also provides a integration of project, database and web server.</p>
<p style="text-align: center;"><img class="size-full wp-image-1504 aligncenter" title="netbeans services server glassfish" src="http://silveiraneto.net/wp-content/uploads/2008/10/netbeans_services_server_glassfish.png" alt="" width="361" height="161" /></p>
<p>Nevertheless, Glassfish has no dependencies with any IDE and perfectly works by alone. If you need <a title="Glassfish on Linux" href="http://silveiraneto.net/2008/09/07/glassfish-on-linux/">I wrote this post</a> explaining how to install and deploy a application on Glassfish from scratch.</p>
<h3>3.2 MySQL Relational Database</h3>
<p style="text-align: center;"><img class="size-full wp-image-1505 aligncenter" title="mysql" src="http://silveiraneto.net/wp-content/uploads/2008/10/mysql.png" alt="" width="180" height="93" /></p>
<p>MySQL is a relational database management system and probably the most used database on internet (has more than 11 million installations). It&#8217;s also very easy to install and administer, through command line or many gui interfaces.</p>
<p>To install MySQL and JDBC driver on Ubuntu just run as root:</p>
<blockquote><p># apt-get install mysql-server libmysql-java</p></blockquote>
<p>After installing and configuring it you can <a title="Looking for databases drivers" href="http://silveiraneto.net/2008/10/13/looking-for-databases-drivers/">test the jdbc driver throught this servlet code</a>. You can optionally register the MySQL on NetBeans to have a easier access to it thought the service tab.</p>
<p style="text-align: center;"><img class="size-full aligncenter" title="netbeans_services_databases" src="http://silveiraneto.net/wp-content/uploads/2008/10/netbeans_services_databases.png" alt="" /></p>
<p>At the command line you can invoke <em>mysql</em> command line interface and use MySql commands or SQL queries. I&#8217;ll login and create a database called xort:</p>
<blockquote><p>$ <strong>mysql -u username -p</strong><br />
Enter password:<br />
Welcome to the MySQL monitor.  Commands end with ; or \g.<br />
Your MySQL connection id is 13<br />
Server version: 5.0.51a-3ubuntu5.3 (Ubuntu)</p>
<p>Type &#8216;help;&#8217; or &#8216;\h&#8217; for help. Type &#8216;\c&#8217; to clear the buffer.</p>
<p>mysql&gt; <strong>create database xort;</strong><br />
Query OK, 1 row affected (0.06 sec)</p></blockquote>
<p>You could also create this database by an SQL statement:</p>
<div class="wp_syntax">
<div class="code">
<pre class="sql sql" style="font-family:monospace;"><span style="color: #993333; font-weight: bold;">CREATE</span> <span style="color: #993333; font-weight: bold;">DATABASE</span> xort;</pre>
</div>
</div>
<p>To select the database xort:</p>
<blockquote><p>mysql&gt; <strong>use xort;</strong><br />
Reading table information for completion of table and column names<br />
You can turn off this feature to get a quicker startup with -A</p>
<p>Database changed<br />
mysql&gt;</p></blockquote>
<p>Now we create a database called pipes with fields pin (pipe in) and pout (pipe out). They represent the input url and the output url of our pipe abstraction.</p>
<div class="wp_syntax">
<div class="code">
<pre class="sql sql" style="font-family:monospace;"><span style="color: #993333; font-weight: bold;">CREATE</span> <span style="color: #993333; font-weight: bold;">TABLE</span> pipes <span style="color: #66cc66;">&#40;</span>
   pin varchar<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">255</span><span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">NOT</span> <span style="color: #993333; font-weight: bold;">NULL</span><span style="color: #66cc66;">,</span>
   pout varchar<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">255</span><span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#41;</span>;</pre>
</div>
</div>
<p>As we expect a lot of searches queries on this table, we can optionally create a index for it on pin field. This can reduce ours searches from O(n) to O(logn) (because pin&#8217;s will be ordered so don&#8217;t need to look all pipes, we can use logn algorithms like binary search).</p>
<div class="wp_syntax">
<div class="code">
<pre class="sql sql" style="font-family:monospace;"><span style="color: #993333; font-weight: bold;">CREATE</span> <span style="color: #993333; font-weight: bold;">INDEX</span> pinindex <span style="color: #993333; font-weight: bold;">ON</span> pipes <span style="color: #66cc66;">&#40;</span>pin<span style="color: #66cc66;">&#41;</span>;</pre>
</div>
</div>
<p>Another trick to improve our speed is recycling connections through connection pools.</p>
<p style="text-align: center;"><img class="size-full wp-image-1535 aligncenter" title="glassfish pool" src="http://silveiraneto.net/wp-content/uploads/2008/10/glassfish_pool.png" alt="" width="300" height="197" /></p>
<p>Creating a pool of MySQL connections on Glassfish is very easy. There&#8217;re two good tutorials on this subject:</p>
<ul>
<li> <a title="Permanent Link: Glassfish v2 UR1 and MySQL Connection Pool" rel="bookmark" href="http://thestewscope.wordpress.com/2008/01/08/glassfish-v2-ur1-and-mysql-connection-pool/">Glassfish v2 UR1 and MySQL Connection Pool</a></li>
<li><a title="Pooling with MySQL" href="http://dev.mysql.com/tech-resources/articles/connection_pooling_with_connectorj.html">Connection pooling with MySQL Connector/J</a></li>
</ul>
<p>And now we populate the database with some initial pipes.</p>
<div class="wp_syntax">
<div class="code">
<pre class="sql sql" style="font-family:monospace;"><span style="color: #993333; font-weight: bold;">INSERT</span> <span style="color: #993333; font-weight: bold;">INTO</span> pipes <span style="color: #993333; font-weight: bold;">VALUES</span> <span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'blog'</span><span style="color: #66cc66;">,</span><span style="color: #ff0000;">'http://silveiraneto.net'</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #993333; font-weight: bold;">INSERT</span> <span style="color: #993333; font-weight: bold;">INTO</span> pipes <span style="color: #993333; font-weight: bold;">VALUES</span> <span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'cejug'</span><span style="color: #66cc66;">,</span><span style="color: #ff0000;">'http://cejug.org/display/cejug/Home'</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #993333; font-weight: bold;">INSERT</span> <span style="color: #993333; font-weight: bold;">INTO</span> pipes <span style="color: #993333; font-weight: bold;">VALUES</span> <span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'orkut'</span><span style="color: #66cc66;">,</span><span style="color: #ff0000;">'http://www.orkut.com.br/Main#Profile.aspx?rl=ls&amp;amp;uid=12443310329436634134'</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #993333; font-weight: bold;">INSERT</span> <span style="color: #993333; font-weight: bold;">INTO</span> pipes <span style="color: #993333; font-weight: bold;">VALUES</span> <span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'glassfish'</span><span style="color: #66cc66;">,</span><span style="color: #ff0000;">'http://glassfish.dev.java.net'</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #993333; font-weight: bold;">INSERT</span> <span style="color: #993333; font-weight: bold;">INTO</span> pipes <span style="color: #993333; font-weight: bold;">VALUES</span> <span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'mysql'</span><span style="color: #66cc66;">,</span><span style="color: #ff0000;">'http://dev.mysql.org'</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #993333; font-weight: bold;">INSERT</span> <span style="color: #993333; font-weight: bold;">INTO</span> pipes <span style="color: #993333; font-weight: bold;">VALUES</span> <span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'twitter'</span><span style="color: #66cc66;">,</span><span style="color: #ff0000;">'http://twitter.com/silveira'</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #993333; font-weight: bold;">INSERT</span> <span style="color: #993333; font-weight: bold;">INTO</span> pipes <span style="color: #993333; font-weight: bold;">VALUES</span> <span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'lab'</span><span style="color: #66cc66;">,</span> <span style="color: #ff0000;">'http://maps.google.com/maps?f=q&amp;amp;geocode=&amp;amp;q=campus+do+pici&amp;amp;g=Fortaleza,+Brazil&amp;amp;ie=UTF8&amp;amp;t=h&amp;amp;ll=-3.745978,-38.574023&amp;amp;spn=0.002452,0.004823&amp;amp;z=18'</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #993333; font-weight: bold;">INSERT</span> <span style="color: #993333; font-weight: bold;">INTO</span> pipes <span style="color: #993333; font-weight: bold;">VALUES</span> <span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'videos'</span><span style="color: #66cc66;">,</span> <span style="color: #ff0000;">'http://br.youtube.com/user/NetoSilveira'</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #993333; font-weight: bold;">INSERT</span> <span style="color: #993333; font-weight: bold;">INTO</span> pipes <span style="color: #993333; font-weight: bold;">VALUES</span> <span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'photos'</span><span style="color: #66cc66;">,</span> <span style="color: #ff0000;">'http://flickr.com/photos/silveiraneto/'</span><span style="color: #66cc66;">&#41;</span>;</pre>
</div>
</div>
<h2>4. Program</h2>
<p>Basically we have just a program that implement this simple behavior:</p>
<ol>
<li>separate the key from the url.</li>
<li>if the key is a pin from a pipe then redirect to that pout.</li>
<li>else provide a way to create a new pipe.</li>
<li>list all pipes.</li>
<li>provide a way to remove a pipe.</li>
</ol>
<p>To get the key we need to separate the proper part of the request uri:</p>
<div class="wp_syntax">
<div class="code">
<pre class="java java" style="font-family:monospace;"><span style="color: #003399;">String</span> uri <span style="color: #339933;">=</span> request.<span style="color: #006633;">getRequestURI</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>;
<span style="color: #003399;">String</span> key <span style="color: #339933;">=</span> uri.<span style="color: #006633;">substring</span><span style="color: #009900;">&#40;</span>request.<span style="color: #006633;">getContextPath</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">length</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">+</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span>;</pre>
</div>
</div>
<p>After that we check if it matches with a pin of some pipe. In this case we redirect user for the correspondent pout:</p>
<div class="wp_syntax">
<div class="code">
<pre class="java java" style="font-family:monospace;">response.<span style="color: #006633;">sendRedirect</span><span style="color: #009900;">&#40;</span>pout<span style="color: #009900;">&#41;</span>;</pre>
</div>
</div>
<p>Notice that using this approach we can connect a url to a extern or intern url (even to another pipe).</p>
<h2>5. Download</h2>
<p>Check out the xort project and sources at <a href="http://xort.dev.java.net/">xort.dev.java.net</a>:</p>
<ul>
<li><a href="https://xort.dev.java.net/servlets/ProjectDocumentList">Releases</a>.</li>
<li><a title="Subversion" href="https://xort.dev.java.net/source/browse/xort/trunk/">Browse Source Code</a>.</li>
</ul>
<p>Or grab sources and the current build with:</p>
<blockquote><p>svn checkout https://xort.dev.java.net/svn/xort/trunk xort</p></blockquote>
<p>Parameters can be passed by the the web.xml file:</p>
<div class="wp_syntax">
<div class="code">
<pre class="xml xml" style="font-family:monospace;">   Set if users can add new pipes using the web interface.
allowNewPipes
true
&nbsp;
   JDBC driver to use
driver
com.mysql.jdbc.Driver
&nbsp;
   Username to login on the database.
username
root
&nbsp;
   Password for the given username.
password
yourpassword
&nbsp;
   JDBC path to database.
database
jdbc:mysql://localhost:3306/xort</pre>
</div>
</div>
<p>Xort up and running:</p>
<p style="text-align: center;"><a href="http://silveiraneto.net/wp-content/uploads/2008/10/xort_running.png"><img class="alignnone size-medium wp-image-1548" title="xort_running" src="http://silveiraneto.net/wp-content/uploads/2008/10/xort_running-500x312.png" alt="" width="500" height="312" /></a></p>
<p><object width="425" height="355"><param name="movie" value="http://www.youtube.com/v/b2Mrk8XfvKk&amp;rel=0&amp;color1=0x3a3a3a&amp;color2=0x999999"></param><param name="wmode" value="transparent"></param><embed src="http://www.youtube.com/v/b2Mrk8XfvKk&amp;rel=0&amp;color1=0x3a3a3a&amp;color2=0x999999" type="application/x-shockwave-flash" wmode="transparent" width="425" height="355"></object></p>
]]></content:encoded>
			<wfw:commentRss>http://silveiraneto.net/2008/10/21/short-urls-with-glassfishmysql/feed/</wfw:commentRss>
		<slash:comments>10</slash:comments>
		</item>
		<item>
		<title>Quem mais comenta no seu blog?</title>
		<link>http://silveiraneto.net/2007/12/04/quem-mais-comenta-no-seu-blog/</link>
		<comments>http://silveiraneto.net/2007/12/04/quem-mais-comenta-no-seu-blog/#comments</comments>
		<pubDate>Wed, 05 Dec 2007 00:34:04 +0000</pubDate>
		<dc:creator>Silveira</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[banco de dados]]></category>
		<category><![CDATA[código]]></category>
		<category><![CDATA[consulta]]></category>
		<category><![CDATA[junção]]></category>
		<category><![CDATA[mysql]]></category>
		<category><![CDATA[sql]]></category>
		<category><![CDATA[tutorial]]></category>
		<category><![CDATA[wordpress]]></category>

		<guid isPermaLink="false">http://silveiraneto.net/2007/12/04/quem-mais-comenta-no-seu-blog/</guid>
		<description><![CDATA[Uma brincadeira de mão na massa com SQL e as tabelas do WordPress. Você vai precisar de: Um blog em WordPress num servidor próprio. Uma lugar onde você possa se conectar ao banco de dados (provavelmente o MySQL). Um terminal SSH no seu servidor ou mesmo o PhpMyAdmin servem. Se você tiver acesso por SSH [...]]]></description>
			<content:encoded><![CDATA[<p>Uma brincadeira de mão na massa com SQL e as tabelas do WordPress.</p>
<p><center><img src='http://silveiraneto.net/wp-content/uploads/2007/12/wordpress-logo.jpg' alt='Logomarca do WordPress' /></center></p>
<p>Você vai precisar de:</p>
<ul>
<li>Um blog em <a href="http://wordpress.org">WordPress</a> num servidor próprio.</li>
<li>Uma lugar onde você possa se conectar ao banco de dados (provavelmente o <a href="http://mysql.com">MySQL</a>). Um terminal SSH no seu servidor ou mesmo o <a href="www.phpmyadmin.net/">PhpMyAdmin</a> servem.</li>
</ul>
<p>Se você tiver acesso por SSH no servidor onde está seu blog, chame o prompt do mysql assim:</p>
<div class="wp_syntax">
<div class="code">
<pre class="bash bash" style="font-family:monospace;">$ mysql <span style="color: #660033;">-u</span> USUÁRIO <span style="color: #660033;">-p</span> <span style="color: #660033;">-h</span> URL_DO_BANCO DATABASE</pre>
</div>
</div>
<p>Trocando USUÁRIO por seu login no mysql, URL_DO_BANCO por a url do seu banco (geralmente <em>127.0.0.1</em>) e DATABASE por a base de dados (geralmente <em>wordpress</em>). Em seguida será perguntada a sua senha.</p>
<p>Uma vez conectado dentro do seu banco de dados você pode executar algumas queries legais.</p>
<p><strong>Os 10 maiores comentaristas do seu blog</strong>: você seleciona da wp_comments (a tabela de comentários do WordPress) selecionando só os nomes e agrupando pelo email do autor e fazendo uma contagem que chamados de quantidade. Também é feito uma ordenação usando o campo contagem. Aqui eu supondo que a pessoa sempre coloque seu próprio email, mas as vezes mude o nome, o que realmente acontece.</p>
<div class="wp_syntax">
<div class="code">
<pre class="sql sql" style="font-family:monospace;"><span style="color: #993333; font-weight: bold;">SELECT</span> comment_author <span style="color: #993333; font-weight: bold;">AS</span> autor<span style="color: #66cc66;">,</span> COUNT<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">*</span><span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">AS</span> quantidade
   <span style="color: #993333; font-weight: bold;">FROM</span> wp_comments
   <span style="color: #993333; font-weight: bold;">GROUP</span> <span style="color: #993333; font-weight: bold;">BY</span> <span style="color: #ff0000;">`comment_author_email`</span>
   <span style="color: #993333; font-weight: bold;">ORDER</span> <span style="color: #993333; font-weight: bold;">BY</span> quantidade <span style="color: #993333; font-weight: bold;">DESC</span>
   <span style="color: #993333; font-weight: bold;">LIMIT</span> <span style="color: #cc66cc;">10</span>
;</pre>
</div>
</div>
<p>Usando isso lá no banco de dados do <a href="http://www.eupodiatamatando.com">eupodiatamatando.com</a> que tem uns 3 mil comentários, deu o seguinte:</p>
<pre>
+---------------------------------------------+------------+
| autor                                       | quantidade |
+---------------------------------------------+------------+
| Silveira                                    |        511 |
| 2007 Janeiro 21 &raquo; Eu Podia Ta Matando       |        174 |
| Kct                                         |        133 |
| Filho                                       |         91 |
| Dora                                        |         83 |
| Leonardo                                    |         67 |
| manero                                      |         58 |
| Esdras                                      |         52 |
| Marlany                                     |         51 |
| Roney Marques                               |         50 |
+---------------------------------------------+------------+
10 rows in set (0.12 sec)
</pre>
<p>Ou seja, em primeiro lugar deu eu mesmo, com 511 comentários. Em segundo, o próprio blog com seus <a href="http://en.wikipedia.org/wiki/Pingback">pingbacks</a>. O maior comentarista mesmo foi o comentarista que se identifica como <a href="http://casadokct.blogspot.com/">Kct</a>.</p>
<p><strong>Os 10 posts mais comentados</strong>: É necessário fazer uma junção entre a tabela dos comentários (wp_comments) e a tabela dos posts (wp_posts) usando o ID do post.</p>
<div class="wp_syntax">
<div class="code">
<pre class="sql sql" style="font-family:monospace;"><span style="color: #993333; font-weight: bold;">SELECT</span> wp_posts<span style="color: #66cc66;">.</span>post_title<span style="color: #66cc66;">,</span> COUNT<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">*</span><span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">AS</span> quantidade
   <span style="color: #993333; font-weight: bold;">FROM</span> wp_comments <span style="color: #993333; font-weight: bold;">JOIN</span> wp_posts
   <span style="color: #993333; font-weight: bold;">ON</span> wp_comments<span style="color: #66cc66;">.</span>comment_post_ID <span style="color: #66cc66;">=</span> wp_posts<span style="color: #66cc66;">.</span>ID
   <span style="color: #993333; font-weight: bold;">GROUP</span> <span style="color: #993333; font-weight: bold;">BY</span> wp_posts<span style="color: #66cc66;">.</span>ID
   <span style="color: #993333; font-weight: bold;">ORDER</span> <span style="color: #993333; font-weight: bold;">BY</span> quantidade <span style="color: #993333; font-weight: bold;">DESC</span>
   <span style="color: #993333; font-weight: bold;">LIMIT</span> <span style="color: #cc66cc;">10</span>
;</pre>
</div>
</div>
<p>A saída que eu tive:</p>
<pre>
+-----------------------------------------+------------+
| post_title                              | quantidade |
+-----------------------------------------+------------+
| Sobre o vôo 3054                        |        104 |
| Chegou o boneco do Capitão Nascimento!  |         80 |
| Vamos trocar links?                     |         74 |
| O celular Linux já está a venda         |         64 |
| O Que o Emo disse pra Ema?              |         57 |
| Bichos grandes. Falsificações?          |         46 |
| Esse ainda tem muito pra upar           |         45 |
| Entenda Heroes                          |         42 |
| Aqui pro iPhone!                        |         40 |
| Ganhe créditos de graça no celular      |         39 |
+-----------------------------------------+------------+
10 rows in set (0.11 sec)
</pre>
<p><strong>Qual o post você mais comentou</strong>: Basta colocar um WHERE e pegar o autor que você quer, no caso, Silveira (eu). Eu coloquei um limite de 10, para pegar só os 10 posts que eu mais comentei.</p>
<div class="wp_syntax">
<div class="code">
<pre class="sql sql" style="font-family:monospace;"><span style="color: #993333; font-weight: bold;">SELECT</span> wp_posts<span style="color: #66cc66;">.</span>post_title<span style="color: #66cc66;">,</span> COUNT<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">*</span><span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">AS</span> quantidade
    <span style="color: #993333; font-weight: bold;">FROM</span> wp_comments <span style="color: #993333; font-weight: bold;">JOIN</span> wp_posts
    <span style="color: #993333; font-weight: bold;">ON</span> wp_comments<span style="color: #66cc66;">.</span>comment_post_ID <span style="color: #66cc66;">=</span> wp_posts<span style="color: #66cc66;">.</span>ID
    <span style="color: #993333; font-weight: bold;">WHERE</span> wp_comments<span style="color: #66cc66;">.</span>comment_author <span style="color: #66cc66;">=</span> <span style="color: #ff0000;">'Silveira'</span>
    <span style="color: #993333; font-weight: bold;">GROUP</span> <span style="color: #993333; font-weight: bold;">BY</span> wp_posts<span style="color: #66cc66;">.</span>ID
    <span style="color: #993333; font-weight: bold;">ORDER</span> <span style="color: #993333; font-weight: bold;">BY</span> quantidade <span style="color: #993333; font-weight: bold;">DESC</span>
    <span style="color: #993333; font-weight: bold;">LIMIT</span> <span style="color: #cc66cc;">10</span>
;</pre>
</div>
</div>
<p>O resultado no meu blog foi:</p>
<pre>
+---------------------------------------------+------------+
| post_title                                  | quantidade |
+---------------------------------------------+------------+
| Vamos trocar links?                         |         20 |
| Regnum, RPG online gratúito                 |         11 |
| Promoção: Estamos Dando 97 dólares!         |          7 |
| Aqui pro iPhone!                            |          6 |
| Vamos escrever um livro?                    |          6 |
| Democracy TV Player                         |          5 |
| Formatura, novidades                        |          5 |
| Como eu consegui cancelar minha conta na Oi |          5 |
| Chico Buarque em Fortaleza                  |          4 |
| Livro Python Guia de Bolso                  |          4 |
+---------------------------------------------+------------+
10 rows in set (0.02 sec)
</pre>
<p>Se você quiser ir mais fundo nas consultas eu recomendo dar uma olhada na <a href="http://dev.mysql.com/doc/refman/5.0/en/select.html">sintaxe do select do Mysql</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://silveiraneto.net/2007/12/04/quem-mais-comenta-no-seu-blog/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
	</channel>
</rss>

