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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T15:09:54+00:00 2026-05-28T15:09:54+00:00

So, I have an active record query that I’m doing in PHP under the

  • 0

So, I have an active record query that I’m doing in PHP under the Codeigniter framework where I want to select all of the entries (named ‘tracks’ below) where a certain value in a join is met (‘tags’ that are associated with the tracks – each track can have multiple tags tied to it). What I’m wondering is if there’s a way to have the query look for tracks that have ALL of the selected tags associated with it. So, instead of having it get all tracks that have a links.tag_id of 1, then get all tracks with a links.tag_id of 2, it returns all tracks that have a links.tag_id of 1 as well as 2. Essentially, it’s a subtractive query where it narrows the chosen results down the more tags that you add. Here’s what I have going thus far:

$this->db->select('tracks.id, 
                        tracks.name AS name, 
                        tracks.filename, 
                        tracks.url_name, 
                        tracks.file_path_high, 
                        tracks.filesize, 
                        tracks.categories,
                        tracks.duration, 
                        tracks.folder,
                        links.tag_id,
                        SUM(links.weight) AS total_weight,
                        tag_data.tag_name');

$this->db->distinct();
$this->db->from('music AS tracks');
$this->db->where_in('links.tag_id', array('1', '2');
$this->db->join('linked_tags AS links', 'links.track_id = tracks.id', 'inner');
$this->db->join('tags AS tag_data', 'tag_data.id = links.tag_id', 'inner');
  • 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-28T15:09:56+00:00Added an answer on May 28, 2026 at 3:09 pm

    What you need is to join your tags table many times for each tag you want to find and use each tags alias in your where clause. For example i’d do this:

    $this->db->select('...');
    $this->db->distinct();
    $this->db->from('music AS tracks');
    //Tag 1
    $this->db->join('linked_tags AS links1', 'links1.track_id = tracks.id', 'inner');
    $this->db->join('tags AS tag_data1', 'tag_data1.id = links1.tag_id AND tag_data1.tag = "tag1"', 'inner');
    //Tag 2
    $this->db->join('linked_tags AS links2', 'links2.track_id = tracks.id', 'inner');
    $this->db->join('tags AS tag_data2', 'tag_data2.id = links2.tag_id AND tag_data2.tag = "tag2"', 'inner');
    //Tag 3
    $this->db->join('linked_tags AS links3', 'links3.track_id = tracks.id', 'inner');
    $this->db->join('tags AS tag_data3', 'tag_data3.id = links3.tag_id AND tag_data3.tag = "tag3"', 'inner');
    

    Obviously, i don’t know what you structure looks like, but you’re a brilliant fellow and will surely understand how to adapt it to your structure…

    What this does is INNER JOIN the LINKED_TAGS and TAGS table many times and forces the INNER JOIN to be done on a specific tag. If the tag cannot be found for a specific object, the inner join will fail and thus will simply not be returned… You can add as many JOINS like these that you want but obviously, at a certain point, it can inccur on query performance.

    For more example on how to use this same technique, view the follow post:

    Advanced MySQL Joining. Speeding up query

    Good luck

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

Sidebar

Related Questions

I have a standard active record model with an attributes that is required: class
I have an active record Song model with a songhash field (string(255)) that contains
I have this query which I want to run in my PHP application back
Using CodeIgniter's Active Record class and MySQL, I have a table of posts with
I'd like to schedule a delayed job that contains an active record query (or
I have a tree of active record objects, something like: class Part < ActiveRecord::Base
I'm tinkering with CodeIgniter and have come across Active Records for the first time.
I've got an active record query which is pretty simple def show user =
Is there a way to use codeigniters active record system to construct a query
I have written an application (using Delphi 2009) that allows a user to select

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.