-2

编辑:更新以包含一些当前的上下文代码,并以粗体添加问题

新手在这里:

我将有 20 个左右的制造商表,每个表中包含 15,000-100,000 行零件。零件编号、替代零件编号、标价、制造价格、销售价格等信息将存储为列。我希望能够开始在搜索框中输入零件编号并让它显示到目前为止与数字匹配的所有零件,然后在我输入更多数字时进行更新。

我目前有一个版本使用 1 个包含 40,000 个零件的表格,当我输入一些数字(例如“0000”)时,它将返回以 0000 开头的前 5 个零件,例如:000011、000013,00015,000023 和 000025 以及关于它们的一些相应信息。如果我再输入一个数字“00002”,它将仅显示 000023 和 000025。但是,我希望此搜索能够遍历数据库中的所有表,以防我不知道哪个制造商制造了该零件。

目前我有一个名为 taft_test1 的数据库,其中有两个名为 ariens_prices 和 briggs_prices 的表。每个表将包含相同的列,但可能不会在每个条目上填充数据。

关于解决此问题的最佳方法的任何想法?

有些人提到 UNION 没问题,但我应该做 20 多个联合,还是有更好的方法来布局数据库?

感谢帮助。

我工作的示例代码:

零件查找.php

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">

<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />

    <title>Taft Power Equipment</title>

    <script type="text/javascript" language="Javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
    <script src="js/jquery.horizontalNav.js"></script>

    <script>
    // When document is ready...
        $(document).ready(function() {

            $('.full-width').horizontalNav({}); // Call horizontalNav on the navigations wrapping element
        });
    </script>


    <script>
        function showUser(str)
        {
            if (str=="")
              {
              document.getElementById("txtHint").innerHTML="";
              return;
              } 
                if (window.XMLHttpRequest)
                  {// code for IE7+, Firefox, Chrome, Opera, Safari
                  xmlhttp=new XMLHttpRequest();
                  }
                else
                  {// code for IE6, IE5
                      xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
                      }
                    xmlhttp.onreadystatechange=function()
                      {
                      if (xmlhttp.readyState==4 && xmlhttp.status==200)
                        {
                        document.getElementById("txtHint").innerHTML=xmlhttp.responseText;
                        }
                      }
                    xmlhttp.open("GET","livesearch.php?q="+str,true);
                    xmlhttp.send();
        }
    </script>


    <link rel="stylesheet" type="text/css" href="style.css" />
    <!--[if lt IE 7]>
        <link rel="stylesheet" type="text/css" href="style-ie.css" />
    <![endif]-->
</head>
<body>
    <div id="page-wrap">
        <div id="inside">

            <div id="header">
                <a href="file:///C:/Users/ryan/Desktop/websites/ryan%20website/index.html"><img src="images/TaftLogo2.png" alt="header" /></a>
            </div>

            <div id="menu">
                <nav class="horizontal-nav full-width horizontalNav-notprocessed">
                    <ul>
                        <li><a href="index.html">Home</a></li>
                        <li><a href="#">Work</a></li>
                        <li><a href="#">Blog</a></li>
                        <li><a href="#">About</a></li>
                        <li><a href="#">Contact</a></li>
                        <li><a href="partslookup.php">Parts Look Up</a></li>
                    </ul>
                </nav>
            </div>

            <div id="main-content">     

                <input id="test" class="auto" type="text" size="25" placeholder="Enter Part Here" onkeyup="showUser(this.value)"        
                <br><br><br>
                <div id="txtHint"><b>Part info will display here.</b></div>         
                <br><br><br><br>    

            </div>

            <div style="clear: both;"></div>

            <div id="footer">
                <p>&copy Copy Right Taft Power Equipment Corp</p>
            </div>
    </div>

</body>

</html>

实时搜索.php

<?php

$q = $_GET['q'];

echo "Part Entered:       $q   <br><br>";


$dbhost = 'localhost:3306';
$dbuser = 'root';
$dbpass = 'password';
$conn = mysql_connect($dbhost, $dbuser, $dbpass);
if(! $conn )
{
  die('Could not connect: ' . mysql_error());
}
$sql = "SELECT * FROM `briggs_prices` WHERE briggs_prices.Description LIKE '$q%' 
        UNION
        SELECT * FROM `ariens_prices` WHERE ariens_prices.Description LIKE '$q%'
        LIMIT 0, 50 ";

mysql_select_db('taft_test1');
$retval = mysql_query( $sql, $conn );
if(! $retval )
{
  die('Could not get data: ' . mysql_error());
}

echo "<table border='1'>
<tr>
<th>Part#</th>
<th>Description</th>
<th>Price</th>
</tr>";
while($row = mysql_fetch_array($retval, MYSQL_ASSOC))
{
  echo "<tr>";
  echo "<tr>";
  echo "<td>" . $row['Description'] . "</td>";
  echo "<td>" . $row['SuperNum'] . "</td>";
  echo "<td>" . $row['LIST1'] . "</td>";
  echo "</tr>";
} 
echo "</table>";




while($row = mysql_fetch_array($retval, MYSQL_ASSOC))
{
    echo "Part# :{$row['Description']}   ".
         "Desc : {$row['SuperNum']}  ".
         "Cost : {$row['LIST1']}  ".
         "--------------------------------<br>";
} 
//echo "Fetched data successfully\n";
mysql_close($conn);
?>
4

3 回答 3

2

您需要更好的数据库布局。就像是:

(P) means Primary Key
(F) means Foreign Key
(I) means Indexed

manufacturers
    id                  unsigned int(P)
    name                varchar(50)
    ...

+----+--------+-----+
| id | name   | ... |
+----+--------+-----+
|  1 | Ariens | ... |
|  2 | Briggs | ... |
| .. | ...... | ... |
+----+--------+-----+

parts
    id                      unsigned int(P)
    number                  char(50)(I)
    manufacturer_id         unsigned int(F manufacturers.id)
    name                    varchar(75)
    superceding_part_id     unsigned int(F parts.id) // Allow NULL
    list_price              double
    manufacturer_price      double
    sale_price              double
    ...

+----+--------+-----------------+----------+---------------------+------------+--------------------+------------+-----+
| id | number | manufacturer_id | name     | superceding_part_id | list_price | manufacturer_price | sale_price | ... |
+----+--------+-----------------+----------+---------------------+------------+--------------------+------------+-----+
|  1 | 000011 |               1 | Widget A | NULL                |       1.00 |                .50 |        .90 | ... |
|  2 | 000012 |               1 | Widget B |                   1 |       2.00 |                .75 |       1.75 | ... |
|  3 | 000013 |               2 | Widget A | NULL                |       1.15 |                .70 |       1.00 | ... |
| .. | ...... | ............... | ........ | ................... | .......... | .................. | .......... | ... |
+----+--------+-----------------+----------+---------------------+------------+--------------------+------------+-----+
于 2013-10-04T18:46:51.050 回答
1

examples$part是您输入的变量。这是查询

$query = 'SELECT TOP 5 PartNumber FROM yourTable WHERE PartNumber LIKE "'.$part.'%"'
于 2013-10-04T07:35:45.667 回答
0

嘿,如果您在所有表中都有相同的列,最好尝试 UNION 从单个查询中获取行。试试

WHERE `PartNumber` LIKE "0001%"
于 2013-10-04T08:06:08.420 回答