我有两个名为company
和customers
的表。
在company
表中,有3个字段ID
,Company
和Type
分别为:
ID Company Type
1 ABC Running
2 XYZ Current
现在再次在
customers
表中提交了公司和客户的值(value),但是在这里,公司的值(value)作为ID
提交为:Company Customer Name
1 monty
2 sandeep
现在我想在客户表中搜索公司名称,但是当我将公司名称放在搜索框中时,它什么也没显示,因为公司名称的值是客户tabe中的ID形式。我该如何实现。
这是我的搜索查询:
$sql = "Select * from customers where name like '%$term%' or location like '%$term%' or company like '%$term%'";
最佳答案
通过 JOIN
列出两个表:
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%'
关于mysql - 从一个表中选择id,从另一个表中选择其值以进行搜索,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/18570021/