ExpressionEngine CMS
Open, Free, Amazing

Thread

This is an archived forum and the content is probably no longer relevant, but is provided here for posterity.

The active forums are here.

Query Module + select entries in a category

March 19, 2012 5:45am

Subscribe [2]
  • #1 / Mar 19, 2012 5:45am

    Citylogic

    26 posts

    Hello, I’m working with the Query module to output channel entries based on a category. With my limited SQL knowledge I am struggling to get to JOIN the category groups.  I have googled and can’t find an eample of this.  Please can you assist.

    I currently have:

    {exp:query 
    sql="SELECT ct.title, ct.url_title 
    FROM exp_channel_titles ct 
    INNER JOIN exp_channel_data cd 
    ON ct.entry_id = cd.entry_id 
    WHERE ct.status = 'open' AND ct.channel_id = '1'"
    }
    {title}
    {/exp:query}

    This is working fine, but returns results accross all categories, how can I amend this to get results from the category_posts table?

    Thank you,
    Aidan

  • #2 / Mar 19, 2012 7:36am

    Boyink!

    5011 posts

    I guess the first question is why are you using a query rather than the channel:entries tag pair with a category parameter?

  • #3 / Mar 19, 2012 7:42am

    Citylogic

    26 posts

    I want to select entries based on the entry_date and expiry_date, being relative to an arbitrary date I input.  I have tried the start_on and stop_before parametres but they use the current date (server time) as the indicator whether an entry has expired.

    With show expired entries set to no, expired entries show if the expiration date is in the future, even if it’s before the stop_before date.

  • #4 / Mar 19, 2012 7:45am

    Boyink!

    5011 posts

    OK.

    One trick that I’ve used in the past is to turn on EE’s output profiler.  This will cause EE to display all the queries it’s running at the bottom of each page. You can then write a quick test template specifying a category id and see what SQL EE generates to perform that.  I typically copy/paste those SQL statements and then use them as a starting point for mine.

  • #5 / Mar 19, 2012 7:49am

    Citylogic

    26 posts

    Thanks! I’ll give that a go.

  • #6 / Mar 19, 2012 8:19am

    Citylogic

    26 posts

    It looks like EE is doing two queries. One on the titles and another on the data, using the entries received from the title query.

    So lastly, is this something I’ll need to replicate in the template using PHP? Or can this be done with one query using the query module?

    0.0004   SELECT DISTINCT(t.entry_id) FROM exp_channel_titles AS t
                    LEFT JOIN exp_channels ON t.channel_id = exp_channels.channel_id LEFT JOIN exp_members AS m ON m.member_id = t.author_id INNER JOIN exp_category_posts ON t.entry_id = exp_category_posts.entry_id
                             INNER JOIN exp_categories ON exp_category_posts.cat_id = exp_categories.cat_id WHERE t.entry_id !='' AND t.site_id IN ('1')  AND t.entry_date < 1332158456  AND (t.expiration_date = 0 OR t.expiration_date > 1332158456) AND t.channel_id = '1' AND exp_categories.cat_id = '6' AND t.status = 'open' AND t.status != 'closed' ORDER BY t.sticky desc, t.entry_date desc, t.entry_id desc LIMIT 0, 100 
    0.0019   SELECT  DISTINCT(t.entry_id),  t.entry_id, t.channel_id, t.forum_topic_id, t.author_id, t.ip_address, t.title, t.url_title, t.status, t.dst_enabled, t.view_count_one, t.view_count_two, t.view_count_three, t.view_count_four, t.allow_comments, t.comment_expiration_date, t.sticky, t.entry_date, t.year, t.month, t.day, t.edit_date, t.expiration_date, t.recent_comment_date, t.comment_total, t.site_id as entry_site_id,
                            w.channel_title, w.channel_name, w.channel_url, w.comment_url, w.comment_moderate, w.channel_html_formatting, w.channel_allow_img_urls, w.channel_auto_link_urls, w.comment_system_enabled, 
                            m.username, m.email, m.url, m.screen_name, m.location, m.occupation, m.interests, m.aol_im, m.yahoo_im, m.msn_im, m.icq, m.signature, m.sig_img_filename, m.sig_img_width, m.sig_img_height, m.avatar_filename, m.avatar_width, m.avatar_height, m.photo_filename, m.photo_width, m.photo_height, m.group_id, m.member_id, m.bday_d, m.bday_m, m.bday_y, m.bio,
                            md.*,
                            wd.*
                    FROM exp_channel_titles        AS t
                    LEFT JOIN exp_channels         AS w  ON t.channel_id = w.channel_id
                    LEFT JOIN exp_channel_data    AS wd ON t.entry_id = wd.entry_id
                    LEFT JOIN exp_members        AS m  ON m.member_id = t.author_id
                    LEFT JOIN exp_member_data    AS md ON md.member_id = m.member_id WHERE t.entry_id IN (15,13,12,7,6,5) ORDER BY t.sticky desc, t.entry_date desc, t.entry_id desc

     

     

  • #7 / Mar 19, 2012 8:31am

    Citylogic

    26 posts

    Wait! I got it working! Woot!

    Thanks!  I’m going to be profiler-ing everything - that stuff’s awesome! 😊

  • #8 / Mar 19, 2012 5:12pm

    Dan Decker

    7338 posts

    Hi Aidan,

    I love the profiler and the template debugger both. Lots of good info in there when things get squirrely!

    I’m glad Mike was able to get you on the right path. If you eve need anything else, just let us know in a new thread.

    Cheers!

.(JavaScript must be enabled to view this email address)

ExpressionEngine News!

#eecms, #events, #releases