6

我有两个名为companyand的表customers

表中company有 3 个字段IDCompanyTypeas:

ID    Company    Type
1     ABC        Running

2     XYZ        Current

现在再次在customers表中提交公司和客户的价值,但这里公司的价值提交ID为:

Company     Customer Name
1              monty

2             sandeep

现在我想在客户表中搜索公司名称,但是当我将公司名称放在搜索框中时,它什么也没有显示,因为公司名称的值是客户表中的 ID 形式。我该如何实现它。

这是我的搜索查询:

$sql = "Select * from customers where name like '%$term%' or location like '%$term%' or company like '%$term%'";
4

2 回答 2

15

通过JOINing 两个表:

Select * 
from customers AS cust
INNER JOIN companies AS comp ON cust.Company = comp.Id
where comp.location like '%$term%' 
   or comp.company like '%$term%'
于 2013-09-02T09:43:25.177 回答
4
try this

SELECT co.*, cu.* FROM company as co, customers as cu where co.company_id = cu.company_id and co.company_name = '%$term%';
于 2013-09-02T09:50:20.160 回答