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

  • SEARCH
  • Home
  • 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 885665
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T12:56:41+00:00 2026-05-15T12:56:41+00:00

I have five tables: tab_template template_group group user_group user Tab_template’s are organized into groups

  • 0

I have five tables:

  1. tab_template
  2. template_group
  3. group
  4. user_group
  5. user

Tab_template’s are organized into groups with the template_group relational table.
Users’s are organized into groups with the user_group relational table.
Group’s can be public or private (using a tinyint boolean column in the table).

I want to query for all of the tab_templates that are either:

  1. In the same group as the user
  2. Or in a Public group

Here is my current query:

SELECT * FROM tab_template t
LEFT JOIN template_group
ON template_group.tab_template_id=t.id
LEFT JOIN group
ON template_group.tab_template_id=group.id
LEFT JOIN user_group
ON TRUE
WHERE
group.private=0
OR
(template_group.group_id=user_group.group_id
AND
user_group.user_id=2)
GROUP BY t.id;

It works, and it’s not SUPER slow per se, but it’s hacky the way I join in the user_group table.

The problem is that I need to JOIN the user_group table for a conditional check, but I only need to do that conditional check IF the group is not private.

I know that instead of the third LEFT JOIN with the ON TRUE condition I could add another table to the FROM clause (FROM tab_template t, user_group ug)… but I can’t do that because the way Yii’s ActiveRecord class works with the DcCriteria I can’t modify that part of the statement. I can edit just about any other part of the query but not the FROM clause. Check out the API here: http://www.yiiframework.com/doc/api/CDbCriteria So that’s why I am JOINing the user_group table the way I am. Some Yii experts might be able to help me solve that problem, but I’m not sure my query will be faster by FROMing the tables instead of the JOINing them anyway.

Any help would be greatly appreciated! Thanks

  • 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-15T12:56:42+00:00Added an answer on May 15, 2026 at 12:56 pm

    My recommendation for situations like these is to use a UNION:

    SELECT t.* 
      FROM TAB_TEMPLATE t
      JOIN TEMPLATE_GROUP tg ON tg.tab_template_id = t.id
      JOIN GROUP g ON g.id = tg.tab_template_id  
                  AND g.private = 0
    UNION 
    SELECT t.* 
      FROM TAB_TEMPLATE t
      JOIN TEMPLATE_GROUP tg ON tg.tab_template_id = t.id
      JOIN GROUP g ON g.id = tg.tab_template_id  
                  AND g.private != 0
      JOIN USER_GROUP ug ON ug.group_id = g.id
    

    Way easier to read, which makes it easier to maintain.

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

Sidebar

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.