简体   繁体   English

MYSQL使用LIKE搜索具有不同列的多个表

[英]MYSQL Searching multiple tables with different columns using LIKE

I need to be able to do a search on multiple tables and return all parts that are similar to the searched term. 我需要能够在多个表上进行搜索,并返回与搜索到的词相似的所有部分。 My problem is that the tables have varying amounts of columns. 我的问题是表格具有不同数量的列。 They do have columns with similar names though. 他们确实有名称相似的列。 First the PHP checks the "productnumber" table to find all parts with the similar part number. 首先,PHP检查“ productnumber”表,以找到具有相似零件编号的所有零件。 Then it searches through each table to find a matching number. 然后,它在每个表中搜索以找到匹配的数字。

Right now if a similar part is found in the Components table, it will not display any parts found from Adapters or Connectors. 现在,如果在“组件”表中找到相似的零件,它将不会显示从适配器或连接器中找到的任何零件。 Instead of selecting all columns and neglecting to search all tables I'd like to search for these three columns which are found in all three tables and return results found in all tables: 与其选择所有列而不是忽略搜索所有表,我想搜索在所有三个表中找到的这三个列,并返回在所有表中找到的结果:

part_num part_num

image 图片

page

if(isset($_GET['num'])) {
$num = $_GET['num'];
$numresult = mysql_query("SELECT * FROM productnumber WHERE part_num LIKE '%$num%'");

  if ($numresult) {

    while ($row = mysql_fetch_array($numresult)) {

        if ($row["title"] == "connectors") {
            $numtitle = "connectors";
            $result = mysql_query("SELECT * FROM connectors WHERE part_num LIKE '%$num%'");
        }

        if ($row["title"] == "adapters") {
            $numtitle = "adapters";
            $result = mysql_query("SELECT * FROM adapters WHERE part_num LIKE '%$num%'");
        }

        if ($row["title"] == "components") {
            $numtitle = "components";
            $result = mysql_query("SELECT * FROM components WHERE part_num LIKE '%$num%'");
        }

    }

  }

}

Any help regarding my question would be greatly appreciated :] 关于我的问题的任何帮助将不胜感激:]

 while ($row = mysql_fetch_array($numresult)) {

        if ($row["title"] == "connectors") {
            $numtitle = "connectors";
            $result[] = mysql_query("SELECT * FROM connectors WHERE part_num LIKE '%$num%'");
        }

        if ($row["title"] == "adapters") {
            $numtitle = "adapters";
            $result[] = mysql_query("SELECT * FROM adapters WHERE part_num LIKE '%$num%'");
        }

        if ($row["title"] == "components") {
            $numtitle = "components";
            $result[] = mysql_query("SELECT * FROM components WHERE part_num LIKE '%$num%'");
        }

    }

you have to make another loop to retrieve data from result array 您必须进行另一个循环才能从结果数组中检索数据

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

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