Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8697239
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T01:28:30+00:00 2026-06-13T01:28:30+00:00

I am adding a tags feature to a polling system using MySql and PHP.

  • 0

I am adding a tags feature to a polling system using MySql and PHP. The user will click a tag to list all polls (AKA propositions) with the respective tag. The link looks like this, “/propositions/tagged/baseball”.

I use a mod_rewrite to assign the “baseball” tag to a variable called $tag.

RewriteRule ^propositions/tagged/(.*)$ /propositions/index.php?tag=$1 [L]

It does not return the tag names of the tags associated with the poll. How would I get those names?

The query below works, but it does not return the tag names of the tags associated with the poll. How would I get those names? Also, can it be done more efficiently. One more thing, would it be better to store the tags with the poll data. I saw a while back where SO does this like including the tags in a table column with the question. Don’t know if they still do that or ever did that but saw or read it somewhere a while back.

Thanks so much!

=======================================================

pb_prop (
  id int(11) NOT NULL auto_increment,
  active tinyint(1) NOT NULL default '1',
  submit_by int(11) NOT NULL,
  total_votes int(11) NOT NULL default '0',
  removed tinyint(1) NOT NULL default '0',
  PRIMARY KEY  (id)
) ENGINE=MyISAM  DEFAULT CHARSET=utf8;

Described below as ".PROP_TABLE." p

=======================================================

pb_prop_tags (
  id int(11) NOT NULL auto_increment,
  prop_id int(11) NOT NULL,
  tag_id int(11) NOT NULL,
  PRIMARY KEY  (id)
) ENGINE=MyISAM  DEFAULT CHARSET=utf8;

Described below as ".PROP_TAGS_TABLE." pt

=======================================================

pb_tags (
  tag_id int(11) NOT NULL auto_increment,
  tag_name varchar(64) NOT NULL,
  PRIMARY KEY  (tag_id)
) ENGINE=MyISAM  DEFAULT CHARSET=utf8;

Described below as ".TAGS_TABLE." t

=======================================================

pb_prop_answers (
  id int(11) NOT NULL auto_increment,
  prop_id int(11) NOT NULL,
  num_votes int(11) NOT NULL default '0',
  PRIMARY KEY  (id),
  KEY poll_id (prop_id)
) ENGINE=MyISAM  DEFAULT CHARSET=utf8;

Described below as ".PROP_ANSWERS_TABLE." a

=======================================================

Working SQL below …could be improved? Since posting the original SQL, I added specific columns to the select …got rid of some wild cards. Also, added more to the WHERE to filter the results better.

Original SQL shown after.

        SELECT
                u.username, u.userid,
                p.*,
                pt.prop_id,
                pt.tag_id,
                t.*,
                (
                    SELECT
                            SUM(num_votes)
                    FROM
                            ".PROP_ANSWERS_TABLE." a
                    WHERE
                            a.prop_id = p.id
                ) AS total_votes,
                (
                    SELECT
                            count(*)
                    FROM
                            ".PROP_ANSWERS_TABLE." a
                    WHERE
                            a.prop_id = p.id
                ) AS total_answers
        FROM
                ".PROP_TABLE." p
        INNER JOIN 
                ".TAGS_TABLE." t
            ON 
                t.tag_name LIKE '%".$tag."%'
        INNER JOIN 
                ".PROP_TAGS_TABLE." pt
            ON 
                pt.tag_id = t.tag_id
        LEFT JOIN
                ".USERS_TABLE." u
            ON
                u.userid = p.submit_by
        WHERE
                pt.tag_id = t.tag_id
            AND
                pt.prop_id = p.id
            AND
                p.removed = 0
            AND
                p.active = 1
            AND
                t.tag_id = pt.tag_id
        ORDER BY
                {$sort} {$dir}

Original posted SQL:

    SELECT
            u.username, u.userid,
            p.*,
            pt.*,
            (
                SELECT
                        SUM(num_votes)
                FROM
                        ".PROP_ANSWERS_TABLE." a
                WHERE
                        a.prop_id = p.id
            ) AS total_votes,
            (
                SELECT
                        count(*)
                FROM
                        ".PROP_ANSWERS_TABLE." a
                WHERE
                        a.prop_id = p.id
            ) AS total_answers
    FROM
            ".PROP_TABLE." p
    INNER JOIN 
            ".PROP_TAGS_TABLE." pt
            ON 
            pt.tag_id = p.id
    INNER JOIN 
            ".TAGS_TABLE." t
            ON 
            t.tag_name LIKE '%".$tag."%'
    LEFT JOIN
            ".USERS_TABLE." u
            ON
            u.userid = p.submit_by
    WHERE
            p.removed = 0
        AND
            p.active = 1
        AND
            t.tag_id = pt.tag_id
    ORDER BY
            {$sort} {$dir}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T01:28:32+00:00Added an answer on June 13, 2026 at 1:28 am

    I think you could do this without the subqueries, just joining against the tables used in the subqueries and using the aggregate functions in the main select. You would need a suitable (and likely quite long!) GROUP BY clause to go with it.

    Something like this (and no doubt there are typos in it)

    SELECT u.username, u.userid,
        p.*,
        pt.*,
        SUM(pat.num_votes) AS calc_total_votes,
        COUNT(pat.num_votes) AS calc_total_answers
    FROM ".PROP_TABLE." p
    INNER JOIN ".PROP_TAGS_TABLE." pt ON pt.tag_id = p.id
    INNER JOIN  ".TAGS_TABLE." t ON t.tag_id = pt.tag_id AND t.tag_name LIKE '%".$tag."%'
    LEFT JOIN ".PROP_ANSWERS_TABLE." pat ON pat.prop_id = p.id
    LEFT JOIN ".USERS_TABLE." u ON u.userid = p.submit_by
    WHERE p.removed = 0
    AND p.active = 1
    GROUP BY u.username, u.userid, p.id, p.active, p.submit_by, p.total_votes , p.removed, pt.id, pt.prop_id, pt.tag_id
    ORDER BY {$sort} {$dir}
    

    Amended SQL below, based on your updated SQL. This is doing a JOIN against a subselect which should be more efficient than a subselect per row.

    SELECT u.username, u.userid, p.*, pt.prop_id, pt.tag_id, t.*, Sub1.total_votes, Sub1.total_answers
    FROM ".PROP_TABLE." p
    INNER JOIN ".PROP_TAGS_TABLE." pt ON pt.prop_id = p.id
    INNER JOIN ".TAGS_TABLE." t ON pt.tag_id = t.tag_id
    LEFT OUTER JOIN (SELECT prop_id, SUM(num_votes) AS total_votes, count(*) AS total_answers
    FROM ".PROP_ANSWERS_TABLE." 
    GROUP BY prop_id ) AS Sub1 ON p.id = Sub1.prop_id
    LEFT OUTER JOIN ".USERS_TABLE." u ON u.userid = p.submit_by
    WHERE t.tag_name LIKE '%".$tag."%'
    AND p.removed = 0
    AND p.active = 1
    ORDER BY {$sort} {$dir}
    

    However it should be possible to do this select with just an overall group by and no need for a subselect.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I use tag-it plugin from https://github.com/aehlke/tag-it/downloads . How to disable adding new tags? $(document).ready(function
I'm adding tags to a label on click by doing the following in jQuery
Would like to implement some solution for adding tags to a certain user profile
I am adding tags using jquery templates to jquery Mobile website. It adds li
anything slider is adding hash tags like #&panel1-1 at the end of the url.
I'm adding html5 video tags to our website to replace the Flash video player
For example what do the around #Trim(FORM.fromfirstname)# do? I'm adding <cfqueryparam...> tags and am
Adding the row one by one in the button click.. (i tried but its
How do I get the ordered numbers bold without adding more tags? Possible in
I'm currently writing a function for parsing some HTML and adding tags where necessary.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.