dive into mark

You are here: dive into markArchivesJuly 2002Fun with SQL

Wednesday, July 31, 2002

Fun with SQL

I upgraded to MT 2.2.1 and MySQL, and I’ve been playing with Brad Choate’s wonderful MT SQL plug-in. Here’s an example of selecting 5 random entries (works in any template):

<ul>
<MTSQLEntries query=”select entry_id from mt_entry order by rand() limit 5″>
<li><a href=”<$MTEntryLink$>”><$MTEntryTitle$></a></li>
</MTSQLEntries>
</ul>

Here’s code to select 5 random entries in the current category (works in Category Archive):

<ul>
<MTSQLEntries query=”select e.entry_id from mt_entry e, mt_placement p, mt_category c where e.entry_id = p.placement_entry_id and p.placement_category_id = c.category_id and c.category_label = ‘&lt;MTArchiveTitle&gt;’ order by rand() limit 5″>
<li><a href=”<$MTEntryLink$>”><$MTEntryTitle$></a></li>
</MTSQLEntries>
</ul>

Here’s code to select the 5 most popular entries, based on the number of trackback pings. I’m not entirely sure about this one, since I had to fake some data to test it and I may have misinterpreted the data model.

<ul>
<MTSQLEntries query=”select trackback_entry_id entry_id, count(*) k from mt_trackback group by entry_id order by k desc limit 5″>
<li><a href=”<$MTEntryLink$>”><$MTEntryTitle$></a></li>
</MTSQLEntries>
</ul>

In all cases, you can change the number after the limit keyword to get more or fewer results.

Did I mention that I really enjoy writing SQL? Expect more of this.

Filed under , , ,

Respond privately

I am no longer accepting public comments on this post, but you can use this form to contact me privately. (Your message will not be published.)



Recent Stuff For You, Special Price Stay Here
  • Greasemonkey Hacks
Good Stuff Buy The Cow Go Away
Dive Into Python
Powered by Google Drink The Milk Don't Steal

 

posts / comments
© 2001-8 Mark Pilgrim