搜索MySQL结果的最佳实践

时间:2015-08-24 10:23:50

标签: php mysql loops

大家好日子。我试图找出在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>";
            }

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

非常感谢

1 个答案:

答案 0 :(得分:0)

好的,所以我使用了如下连接建议:

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"

然后将该优惠券的最新互动放在顶部。 感谢所有的帮助!