在MySQL结果中搜索的最佳实践


Best practise to search in MySQL result

大家好。我试图找出什么是最好的方式来搜索内的选择语句。我现在有下面的select语句:

$sql_grn = "SELECT fl_voucher_no, fl_voucher_grn, tb_customers.fl_cust_name "
                    . "FROM tb_vouchers "
                    . "Join tb_customers on tb_vouchers.fl_voucher_customer = tb_customers.fl_cust_accno "
                    . "WHERE fl_voucher_grn LIKE  '%$grn%'";

返回所有链接到搜索的grn的凭证号。他们现在希望在另一个表中显示凭证的状态。

我正在考虑在while循环中放入另一个mysql查询:

while($row = mysql_fetch_assoc($result))
            {   
                /*another mysql query here finding the status based on the voucher number*/
                echo "<tr><td>$row[fl_voucher_no]</td><td>$row[fl_voucher_grn]</td><td>$row[fl_cust_name]</td></tr>";
            }

这是正确的方法,还是有更好的方法,更容易对数据库或服务器?

Thank you so much

我使用的连接建议如下:

SELECT fl_voucher_no, fl_voucher_grn, tb_customers.fl_cust_name, tb_accepted.fl_claim_status, tb_accepted.fl_date, tb_accepted.fl_rate FROM tb_vouchers
                        Join tb_customers on tb_vouchers.fl_voucher_customer = tb_customers.fl_cust_accno 
                        left Join tb_accepted ON tb_vouchers.fl_voucher_no = tb_accepted.fl_voucher
                        WHERE fl_voucher_grn LIKE '%$grn%'
                        Order by tb_accepted.fl_date desc"

然后将该凭证最近的交互放在顶部。谢谢你的帮助!