MYSQL Searching multiple tables with different columns using LIKE

坚强是说给别人听的谎言 提交于 2019-12-11 07:55:46

问题


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. 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

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 :]


回答1:


 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



来源:https://stackoverflow.com/questions/12305931/mysql-searching-multiple-tables-with-different-columns-using-like

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!