php - 使用 PHP、MYSQL、GROUP_CONCAT 和 JOIN 进行空间搜索

标签 php mysql search join group-concat

我对代码进行了一些更改,现在它可以正常工作了。但是有一个新的问题。 在数据库中,我注册了 3 个公司代码:G-1001、G-1002 和 G-1004。

如果我键入“g”、“g-”或例如“g-100”并单击搜索,它会正确返回所有结果。但是,如果我只键入“g-1001”甚至“01”、“001”,它不会返回任何结果。

为什么我搜索注册码的最后一个字符时不起作用?

<?php  
if(isset($_POST['action']) && $_POST['action'] == 'send'){
    $codsearch = preg_replace('#[^a-z 0-9?()!-]#i', '', $_POST['searchcompanycod']);

    $getresultsquery = mysql_query('SELECT p.*, a.idProduct, a.idVehic, a.year, v.nameVehic, GROUP_CONCAT(a.year SEPARATOR "<br>"), GROUP_CONCAT(a.idVehic SEPARATOR "<br>"), GROUP_CONCAT(v.nameVehic SEPARATOR "<br>")
    FROM products p
    INNER JOIN application a ON p.idProduct = a.idProduct
    INNER JOIN vehic v ON a.idVehic = v.idVehic
    WHERE codCompany LIKE "%'.$codsearch.'%"
    GROUP BY p.codCompany') or die(mysql_error());

    $resultsrow = mysql_num_rows($getresultsquery);
    if($resultsrow > 1){
        echo "$codsearch";
        echo "<table class='table table-bordered' border=1>";
        echo "<tr>";
        echo "<th>Company Code</th><th>Original Code</th><th>Descr.</th><th>idProduct</th><th>idVehic</th><th>Vehic Name</th><th>Year</th>";
        echo "</tr>";

        while($getresultsline = mysql_fetch_array($getresultsquery)) {
            echo "<tr>";  
            echo "<td>" . $getresultsline['codCompany'] . "</td>";
            echo "<td>" . $getresultsline['codOriginal'] . "</td>";
            echo "<td>" . $getresultsline['typeDesc'] . "</td>";
            echo "<td>" . $getresultsline['idProduct'] . "</td>";
            echo "<td>" . $getresultsline['GROUP_CONCAT(a.idVehic SEPARATOR "<br>")'] . "</td>";
            echo "<td>" . $getresultsline['GROUP_CONCAT(v.nameVehic SEPARATOR "<br>")'] . "</td>";
            echo "<td>" . $getresultsline['GROUP_CONCAT(a.year SEPARATOR "<br>")'] . "</td>";  
            echo "</tr>";
        }
    }   else{
            echo "No results";}
}
?>  
</tbody>
</table>

有什么想法吗? 我的测试页面是:http://flyingmail.com.br/test/produtos.php (仅公司代码过滤器有效)

最佳答案

您的 SQL 上的突出显示应该向您显示主要问题。

改变

'SELECT table1.*, table2.productID, table2.veicleID, table2.date, table3.veicleNAME, GROUP_CONCAT(table2.date SEPARATOR "<br>"), GROUP_CONCAT(table3.veicleNAME SEPARATOR "<br>")
    FROM table1
    INNER JOIN table2 ON table1.productID = table2.productID
    INNER JOIN table3 ON table2.veicleID = table3.veicleID
    GROUP BY productID
    HAVING productID LIKE '%".$codsearch."%''

"SELECT table1.*, table2.productID, table2.veicleID, table2.date, table3.veicleNAME, GROUP_CONCAT(table2.date SEPARATOR "<br>"), GROUP_CONCAT(table3.veicleNAME SEPARATOR "<br>")
    FROM table1
    INNER JOIN table2 ON table1.productID = table2.productID
    INNER JOIN table3 ON table2.veicleID = table3.veicleID
    GROUP BY productID
    HAVING productID LIKE '%".$codsearch."%'"

(注意我把第一个和最后一个单引号改成了双引号)

关于php - 使用 PHP、MYSQL、GROUP_CONCAT 和 JOIN 进行空间搜索,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34166866/

相关文章:

SQL COUNT 返回使用特定属性的项目数

algorithm - 在插值搜索中计算中间值?

PHP 无需爬行 Google 即可获取网站的 Google 排名

javascript - 如何在刷新页面后保持复选框处于选中状态

php - 多边形算法/伪代码中的最短路径

php - 警告 : ftp_login(): User cannot log in, 主目录不可访问

mysql - 我可以在不锁定任何内容的情况下更改 mysql 外键引用的表吗?

php - SQL从表中选择不同的最后一个值

algorithm - 如何通过归纳法证明二分查找是正确的呢?

php - 为数据库中的每个 ID 维护一个字符串队列