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 545809
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T10:48:04+00:00 2026-05-13T10:48:04+00:00

I’m trying to create a efficient query which will retrieve ‘projects’ from a database

  • 0

I’m trying to create a efficient query which will retrieve ‘projects’ from a database and all the relevant ‘tags’ which have been assigned to that particular project.

My table setup mimics that of wordpress, and is a tad complicated:

term
- Where I define the tags name, slug etc

term_relationships
- Links the project to a term in the above table.

term_taxonomy
- Defines the taxonomy of each of terms in the term table e.g. 'category', 'tag'.

So, what I need to do is first query the term_taxonomy table for any entries with the taxonomy ‘tag’, then filter these results so that only the terms specified in term_relationships are returned. Finally I then need to query the term table to return the relevant tags.

My question is which is fastest way of doing this? I imagine it would be best to split it into multiple queries eg. Query 1: retrieve project, Query 2: retrieve project tags but please consider I may have up to 20-30 projects per page.

PS: I know it’d be far easier to just create a new table specifically for tagging, but I’d like to confine it to my current setup for now.

Any help would be much appreciated, as this is driving me mad!

  • 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-05-13T10:48:04+00:00Added an answer on May 13, 2026 at 10:48 am
    select     p.id
    ,          p.name 
    ,          group_concat(t.text order by t.text) as tags
    from       project              p
    left join  term_relationships   tr
    on         p.id               = tr.project_id
    left join  term                 t
    on         tr.term_id         = t.id 
    left join  term_taxonomy        tt
    on         t.term_taxonomy_id = tt.id
    and        'tag'              = tt.type
    group by   p.id
    

    note that the group by clause does not include all selected columns from project – other databases will bitch about this, but Msql won’t. Also note that it is perfectly valid to do so in this case, as p.id is the primary key of project, so there can be only one distinct name value per p.id.

    One word of caution RE GROUP_CONCAT():

    The result is truncated to the maximum length that is given by the group_concat_max_len system variable, which has a default value of 1024. The value can be set higher, although the effective maximum length of the return value is constrained by the value of max_allowed_packet. The syntax to change the value of group_concat_max_len at runtime is as follows, where val is an unsigned integer:

    SET [GLOBAL | SESSION] group_concat_max_len = val;

    See http://dev.mysql.com/doc/refman/5.0/en/group-by-functions.html#function_group-concat

    So decide in advance how large the list of tags may be, and set @@group_concat_max_len to that value.

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

Sidebar

Ask A Question

Stats

  • Questions 368k
  • Answers 368k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer The Entity Framework designer is terrible - I've had the… May 14, 2026 at 5:11 pm
  • Editorial Team
    Editorial Team added an answer If by "hijack" you meant sniff the packets then what… May 14, 2026 at 5:11 pm
  • Editorial Team
    Editorial Team added an answer If you want two actions to be atomic, embed them… May 14, 2026 at 5:11 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.