【问题标题】:Select id from one table and its value from another table to search从一个表中选择 id 并从另一个表中选择它的值进行搜索
【发布时间】:2013-09-05 08:58:35
【问题描述】:

我有两个名为 companycustomers 的表。

company 表中有3 个字段IDCompanyType

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%'";

【问题讨论】:

  • Monty,你应该看看 JOINS in SQL。它们非常重要,您肯定会一直使用它们。您可以通过此链接开始使用w3schools.com/sql/sql_join.asp
  • @medina: 非常感谢这个宝贵的建议。

标签: mysql select where sql-like


【解决方案1】:

通过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%'

【讨论】:

    【解决方案2】:
    try this
    
    SELECT co.*, cu.* FROM company as co, customers as cu where co.company_id = cu.company_id and co.company_name = '%$term%';
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-02-23
      • 2021-12-30
      • 2020-08-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-03-23
      相关资源
      最近更新 更多