如何搜索数据库中的所有表?

时间:2013-10-04 07:30:30

标签: php mysql database

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

新手:

我将有20张左右的制造商表,每张表中都有15,000-100,000行零件。 PartNumber,Superceding Part Number,List Price,MFG价格,销售价格等信息将作为列存储。我希望能够在搜索框中输入一个部件号,并让它显示到目前为止匹配数字的所有部分,然后在输入更多数字时更新。

我目前有一个版本使用1个40,000个零件的表格,当我键入一些数字,如'0000'时,它将返回以0000开头的前5个部分,例如:000011,000013,00015,000023,和000025以及它们的一些相应信息。如果我再输入一个数字'00002',它将只显示000023和000025.但是我希望这个搜索能够在数据库中的所有表格上运行,以防我不知道哪个制造商成功。

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

有关最佳方式的任何想法吗?

有些人提到UNION哪个好,但我应该做20多个工会还是有更好的方法来布局数据库?

感谢帮助。

示例代码我工作:

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

livesearch.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);
?>

3 个答案:

答案 0 :(得分: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 | ... |
| .. | ...... | ............... | ........ | ................... | .......... | .................. | .......... | ... |
+----+--------+-----------------+----------+---------------------+------------+--------------------+------------+-----+

答案 1 :(得分:1)

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

$query = 'SELECT TOP 5 PartNumber FROM yourTable WHERE PartNumber LIKE "'.$part.'%"'

答案 2 :(得分:0)

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

WHERE `PartNumber` LIKE "0001%"