<?xml version='1.0' encoding='UTF-8'?><?xml-stylesheet href="http://www.blogger.com/styles/atom.css" type="text/css"?><feed xmlns='http://www.w3.org/2005/Atom' xmlns:openSearch='http://a9.com/-/spec/opensearchrss/1.0/' xmlns:blogger='http://schemas.google.com/blogger/2008' xmlns:georss='http://www.georss.org/georss' xmlns:gd="http://schemas.google.com/g/2005" xmlns:thr='http://purl.org/syndication/thread/1.0'><id>tag:blogger.com,1999:blog-7703436087329130546</id><updated>2024-09-05T18:38:01.489-07:00</updated><title type='text'>Convert MySQL Queries to RoR query</title><subtitle type='html'></subtitle><link rel='http://schemas.google.com/g/2005#feed' type='application/atom+xml' href='http://krunal-mysql-query-to-ror.blogspot.com/feeds/posts/default'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7703436087329130546/posts/default?redirect=false'/><link rel='alternate' type='text/html' href='http://krunal-mysql-query-to-ror.blogspot.com/'/><link rel='hub' href='http://pubsubhubbub.appspot.com/'/><author><name>Krunal Shah</name><uri>http://www.blogger.com/profile/08229248961934158256</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='26' src='//blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiOoLKB-OqA1JM_6WOXkJ9SwZ8HTuR3SwwzynCRC99r0_l-rSzQNOGDQBLFmnXxe0jsiHOe5UPpC2tcPmMdbqm5DKKwEoQkz1CG-woP6PefcirewjDEcv9L3jmmARMMoSI/s220/kru.jpg'/></author><generator version='7.00' uri='http://www.blogger.com'>Blogger</generator><openSearch:totalResults>1</openSearch:totalResults><openSearch:startIndex>1</openSearch:startIndex><openSearch:itemsPerPage>25</openSearch:itemsPerPage><entry><id>tag:blogger.com,1999:blog-7703436087329130546.post-1280886723518745175</id><published>2009-11-27T21:04:00.000-08:00</published><updated>2009-12-03T02:58:08.209-08:00</updated><title type='text'>Using Joins with MySQL Queries and Convert to RoR</title><content type='html'>Using Joins on MySQL queries and then convert to RoR.&lt;br /&gt;&lt;br /&gt;&lt;span style=&quot;color: rgb(51, 51, 255); font-weight: bold; font-style: italic;font-size:180%;&quot; &gt;1) Self Join with Rails.&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style=&quot;font-size:130%;&quot;&gt;Table Name - &quot;Comment&quot;,&lt;br /&gt;Attributes - &quot;Id,Comment_Detail,Parent_Id&quot;&lt;br /&gt;&lt;br /&gt;Sample Comment Data&lt;br /&gt;----------------------------------------------------------&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;font-size:130%;&quot;&gt;        &lt;span style=&quot;color: rgb(0, 102, 0);&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: rgb(0, 102, 0);font-family:georgia;&quot; &gt;&lt;span style=&quot;color: rgb(204, 0, 0);&quot;&gt;||  Id    ||  Comment_Detail || Parent_Id&lt;/span&gt;&lt;br /&gt;-----------------------------------------------------------&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;font-size:130%;&quot;&gt;||   1   ||    Krunal     ||  nil&lt;br /&gt;&lt;/span&gt;&lt;span style=&quot;font-size:130%;&quot;&gt;&lt;br /&gt;||   2   ||    Nishant     ||  1&lt;br /&gt;&lt;/span&gt;&lt;span style=&quot;font-size:130%;&quot;&gt;&lt;br /&gt;||   3   ||    Nirali      ||  nil&lt;br /&gt;&lt;/span&gt;&lt;span style=&quot;font-size:130%;&quot;&gt;&lt;br /&gt;||   4   ||    Manan       ||  3&lt;br /&gt;&lt;/span&gt;&lt;span style=&quot;font-size:130%;&quot;&gt;&lt;br /&gt;||    5   ||    Ankit      ||  nil&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;----------------------------------------------------------------&lt;br /&gt;&lt;ul style=&quot;color: rgb(51, 51, 51);&quot;&gt;&lt;li&gt;&lt;span style=&quot;font-size:130%;&quot;&gt;&lt;span style=&quot;font-weight: bold; font-style: italic; color: rgb(51, 204, 0);&quot;&gt;Query&lt;/span&gt;&lt;span style=&quot;color: rgb(51, 204, 0);&quot;&gt; :&lt;/span&gt;-&gt; To fetch only parent comments which has nil parent_id and also want count of  their child comments.&lt;/span&gt;&lt;/li&gt;&lt;/ul&gt; &lt;span style=&quot;color: rgb(0, 204, 204); font-weight: bold;font-size:180%;&quot; &gt;MySql Syntax :-&gt;&lt;/span&gt;&lt;span style=&quot;font-size:130%;&quot;&gt;&lt;br /&gt;&lt;br /&gt;    (SELECT b.*,(select count(*) from comments c where c.parent_id = b.id ) as commentcount FROM `comments` b WHERE (parent_id is NULL) ORDER BY created_at DESC LIMIT 5)&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;           &lt;span style=&quot;color: rgb(204, 0, 0); font-weight: bold;font-size:180%;&quot; &gt;Conversion to Rails Syntax :-&gt; &lt;/span&gt;&lt;span style=&quot;font-size:130%;&quot;&gt;&lt;br /&gt;&lt;br /&gt;    Comment.find(:all,:select =&gt; &quot;b.*, (select count(*) from comments c where c.parent_id = b.id) as count&quot;, :conditions =&gt; [&quot;parent_id is NULL)&quot;], :from =&gt; &quot;comments as b&quot;,:order =&gt; &quot;b.created_at DESC&quot;,:limit =&gt; 5)&lt;br /&gt;&lt;br /&gt;&lt;span style=&quot;font-size:180%;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;           &lt;span style=&quot;color: rgb(51, 102, 255); font-weight: bold; font-style: italic;font-size:180%;&quot; &gt;2) Another Query&lt;/span&gt;&lt;span style=&quot;font-size:130%;&quot;&gt;&lt;br /&gt;&lt;br /&gt;Table Name &quot;Video&quot;, Attributes &quot;Id,title,created_at,updated_at&quot;&lt;br /&gt;Table Name &quot;Views&quot;, Attributes &quot;Id,Viewable_type,Viewable_id&quot;&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;&lt;ul&gt;&lt;li&gt;&lt;span style=&quot;font-size:130%;&quot;&gt; &lt;span style=&quot;font-weight: bold; font-style: italic; color: rgb(51, 204, 0);font-size:180%;&quot; &gt;Relation&lt;/span&gt;&lt;span style=&quot;color: rgb(51, 204, 0);font-size:180%;&quot; &gt; &lt;/span&gt;:-&gt; One video has many views.&lt;/span&gt;&lt;br /&gt;&lt;/li&gt;&lt;/ul&gt;&lt;span style=&quot;font-size:130%;&quot;&gt;&lt;/span&gt;&lt;ul&gt;&lt;li&gt;&lt;span style=&quot;font-size:130%;&quot;&gt;&lt;span style=&quot;font-weight: bold; font-style: italic; color: rgb(51, 204, 0);font-size:180%;&quot; &gt;Query&lt;/span&gt; :-&gt; To fetch videos with it&#39;s view count.&lt;/span&gt;&lt;/li&gt;&lt;/ul&gt;&lt;span style=&quot;font-size:130%;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style=&quot;color: rgb(102, 204, 204); font-weight: bold;font-size:180%;&quot; &gt;MySql Syntax :-&gt;&lt;/span&gt;&lt;span style=&quot;font-size:130%;&quot;&gt;&lt;span style=&quot;font-size:180%;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;(select a.*,(select count(*) from views where viewable_type = &#39;video&#39; and viewable_id = a.id ) as videosviews from videos a order by videosviews DESC LIMIT 15)&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style=&quot;font-weight: bold; color: rgb(204, 0, 0);font-size:180%;&quot; &gt;Conversion to Rails Syntax :-&gt;&lt;/span&gt;&lt;span style=&quot;font-size:130%;&quot;&gt;&lt;span style=&quot;font-size:180%;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;Video.find(:all,:select =&gt; &quot;a.*,(select count(*) from views where viewable_type = &#39;video&#39; and viewable_id = a.id ) as viewscount&quot;,:order =&gt; &quot;viewscount DESC&quot;, :limit =&gt; 15,:from =&gt; &quot;videos as a&quot;)&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;</content><link rel='replies' type='application/atom+xml' href='http://krunal-mysql-query-to-ror.blogspot.com/feeds/1280886723518745175/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://krunal-mysql-query-to-ror.blogspot.com/2009/11/using-joins-with-mysql-queries-and.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7703436087329130546/posts/default/1280886723518745175'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7703436087329130546/posts/default/1280886723518745175'/><link rel='alternate' type='text/html' href='http://krunal-mysql-query-to-ror.blogspot.com/2009/11/using-joins-with-mysql-queries-and.html' title='Using Joins with MySQL Queries and Convert to RoR'/><author><name>Krunal Shah</name><uri>http://www.blogger.com/profile/08229248961934158256</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='26' src='//blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiOoLKB-OqA1JM_6WOXkJ9SwZ8HTuR3SwwzynCRC99r0_l-rSzQNOGDQBLFmnXxe0jsiHOe5UPpC2tcPmMdbqm5DKKwEoQkz1CG-woP6PefcirewjDEcv9L3jmmARMMoSI/s220/kru.jpg'/></author><thr:total>0</thr:total></entry></feed>