简体   繁体   English

从多个表中查询数据

[英]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 . 我要另外搜索的表称为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 . 您可能想稍微复习一下SQL ,尤其是看看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 . 首先,您可能想研究使用LIKE而不是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. 似乎您正在使用ORM /框架来访问数据,而您正在使用的是哪一个。

Anyway, you are probably looking for join or union . 无论如何,您可能正在寻找joinunion

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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