简体   繁体   中英

query data from more than one table

I have a search function that currently grabs data from one table, and I'd like to grab data from an additional table, as well.

$query = $this->db->get('tbl_customer');
$this->db->select('in_customer_id, st_company_name, in_customer_type, st_customer_account, st_customer_state_id, flg_customer_account_type, in_status, dt_added_date, st_tag');
if(trim($action['searchtxt'])!='')
    $this->db->like('st_company_name', html_entity_decode($action['searchtxt']));

The view:

   <div class="floatl"  style="width:250px;">
      <form name="frm" action="<?php echo $index_url; ?>customers/search/" method="post">
        <div class="floatl">                
          <input name="Search" type="text" class="textboxsearch" id="Search" onfocus="if(this.value==this.defaultValue)this.value='';" onblur="if(this.value=='')this.value=this.defaultValue;" value="<?php if($searchtxt!=''){ echo $searchtxt; } else{ echo 'Search'; }?>" maxlength="50" />
        </div>
            <div class="floatl searchicon">
          <input type="image"  src="<?=$admin_base_url?>images/textbox_search.gif" alt="" width="22" height="22" />
          </div>
        <br />
        <br />
        <font class="txt9">(i.e. Company, Account name)</font>
      </form>          
    </div>

The table I want to additionally search is called tbl_admin_user . Any ideas on how I can accomplish this?

You might want to brush up on your SQL a little, especially take a look at SQL Joins .

With that said, after re-reading your question it appears that you are attempting to search particular columns for fairly specific data within multiple tables. First you might want to look into using LIKE instead of WHERE .

Second, depending on how you're displaying the results you you'll probably either write two separate queries and then loop through each of the separate results and display them. A Union or Join might be possible but also might be difficult to display the results accurately if the table structures are really different.

It seems that you are using an ORM/framework to access data, which one are you using.

Anyway, you are probably looking for join or union .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM