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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T20:45:02+00:00 2026-05-25T20:45:02+00:00

I am wanting to write this query: SELECT u.userId, uil.interestId, url.regionId FROM users u

  • 0

I am wanting to write this query:

SELECT u.userId, uil.interestId, url.regionId FROM users u 
  JOIN userInterestLink uil USING (userId)
  JOIN userRegionLink url USING (userId)
  JOIN dealInterestLink dil USING (interestId)
  JOIN dealRegionLink drl USING (regionId, dealId)
WHERE dealId = 1

using the code igniter active records class, however I have no ideda how to do the JOIN ... USING

  • 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-25T20:45:03+00:00Added an answer on May 25, 2026 at 8:45 pm

    There is no built-in support for JOIN ... USING in the active record class. Your best bet would probably change the join() function to be like this (the file is system/database/DB_active_rec.php in case you don’t know)

    public function join($table, $cond, $type = '')
    {
        if ($type != '')
        {
            $type = strtoupper(trim($type));
    
            if ( ! in_array($type, array('LEFT', 'RIGHT', 'OUTER', 'INNER', 'LEFT OUTER', 'RIGHT OUTER')))
            {
                $type = '';
            }
            else
            {
                $type .= ' ';
            }
        }
    
        // Extract any aliases that might exist.  We use this information
        // in the _protect_identifiers to know whether to add a table prefix
        $this->_track_aliases($table);
    
        // Strip apart the condition and protect the identifiers
        if (preg_match('/([\w\.]+)([\W\s]+)(.+)/', $cond, $match))
        {
            $match[1] = $this->_protect_identifiers($match[1]);
            $match[3] = $this->_protect_identifiers($match[3]);
    
            $cond = $match[1].$match[2].$match[3];
        }
    
        // Assemble the JOIN statement
        $type.'JOIN '.$this->_protect_identifiers($table, TRUE, NULL, FALSE);
    
        $using_match = preg_match('/using[ (]/i', $cond);
    
        if ($using_match)
        {
            $join .= $cond;
        }
        else
        {
            $join .= ' ON '.$cond;
        }
    
        $this->ar_join[] = $join;
        if ($this->ar_caching === TRUE)
        {
            $this->ar_cache_join[] = $join;
            $this->ar_cache_exists[] = 'join';
        }
    
        return $this;
    }
    

    So then, you can simply use this in your code join('table', 'USING ("something")')

    Although, you might want to extend the class instead of modifying it so that you won’t need to do same thing over and over again when you upgrade your CI.
    Have a look at this article or this one (or search google) if you want to do so instead.

    Or if you don’t want to go all those troubles, you can write a simple helper function that can do the same thing.

    <?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
    
    function join_using($table, $key) 
    {
        $CI = get_instance();
        $join = 'JOIN '. $table .' USING (`'. $key .'`)';
        return $CI->db->ar_join[] = $join;
    }  
    

    Later on, just load the helper and call the function like this join_using('table', 'key'). It will then produce the same result as you would with the original join() except this one will give you USING instead of ON condition.

    For example:

    // $something1 and $something2 will produce the same result.
    $something1 = $this->db->join('join_table', 'join_table.id = table.id')->get('table')->result();
    print_r($something1);
    join_using('join_table', 'id');
    $something2 = $this->db->get('table')->result();
    print_r($something2);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i want to make a new data structure using arrays for this query it
I am wanting to rewrite a url like: http://my.project/mydomain.com/ANY_NUMBER_OF_CATEGORIES/designer/4/designer-name/page.html to this: http://my.projects/mydomain.com/ANY_NUMBER_OF_CATEGORIES/page.html?designer=4 I would
I'm wanting to optimize a query using a union as a sub query. Im
I am wanting to write some web services using WCF. I would like to
I am wanting to write a small application that can pull RSS feeds from
I find myself often wanting to write Python list comprehensions like this: nearbyPoints =
I'm a linux noob wanting to migrate this piece of C# code: using System;
I'm wanting to write a method that I can use to initialise a Map.
I'm wanting to write a virtual keyboard, like windows onscreen keyboard for touchscreen pcs.
Often, I find myself wanting to write a unit test for a portion of

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.