我正在尝试查询并按特定列对搜索结果排序。
选择搜索结果:

$query = "SELECT *
          FROM `orders`
          WHERE CONCAT(`KeyAccount`, `BatchNumber`, `Product`, `Quantity`, `PO`, `DateRequested`, `DateDelivered`, `Status`, `Serial`, `Voucher`, `DateExpiry`) LIKE '%".$valueToSearch."%'";
$search_result = filterTable($query);

完整搜索结果源代码:
<?php

if(isset($_GET['search']))
{
    $valueToSearch = $_GET['valueToSearch'];
    // search in all table columns
    // using concat mysql function
    $query = "SELECT *
              FROM `orders`
              WHERE CONCAT(`KeyAccount`, `BatchNumber`, `Product`, `Quantity`, `PO`, `DateRequested`, `DateDelivered`, `Status`, `Serial`, `Voucher`, `DateExpiry`) LIKE '%".$valueToSearch."%'";
    $search_result = filterTable($query);
}
else {
    $query = "SELECT * FROM `orders`";
    $search_result = filterTable($query);
}

// function to connect and execute the query
function filterTable($query)
{
    $connect = mysqli_connect("localhost", "root", "", "test");
    $filter_Result = mysqli_query($connect, $query);
    return $filter_Result;
}

?>

我在考虑使用ORDER BY,或者使用join?
虽然不是家庭主妇,但任何帮助都是值得感谢的!

最佳答案

必须在查询结束时添加:

order by 1 asc;

1是列号,也可以写列名
order by Product asc;

或者你可以做多种排序
order by Product asc, KeyAccount desc;

检查this

关于php - PHP/MySQL-MySQL查询以按特定列对结果进行排序,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/51319965/

10-09 08:35
查看更多