从多个表中查询数据
我有一个搜索功能,当前可以从一个表中获取数据,并且我也想从另一个表中获取数据。
$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']));
视图:
<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>
我想要另外搜索的表名为tbl_admin_user
。关于如何实现这一目标有什么想法吗?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可能想稍微温习一下 SQL,特别是看看 SQL Joins 。
话虽如此,重新阅读您的问题后,您似乎正在尝试在多个表中搜索特定列以获取相当具体的数据。首先,您可能想考虑使用 LIKE 而不是 哪里。
其次,根据显示结果的方式,您可能会编写两个单独的查询,然后循环遍历每个单独的结果并显示它们。联合或联接可能是可能的,但如果表结构确实不同,也可能难以准确显示结果。
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.
看来您正在使用 ORM/框架来访问数据,您使用的是哪一个。
无论如何,您可能正在寻找 join 或 联盟。
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.