【问题标题】:Best practise to search in MySQL result在 MySQL 结果中搜索的最佳实践
【发布时间】:2015-08-24 10:23:50
【问题描述】:

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

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

非常感谢

【问题讨论】:

  • 分享包含凭证状态的表格的详细信息。
  • 最好是让 MySQL 为您完成工作,并且只返回您需要的内容。如果您返回未使用的数据,那是一种浪费
  • 谢谢各位,Dave 是不是意味着要执行嵌套的 select 语句?
  • 我会在嵌套的 select 语句之前加入另一个连接,这取决于您的表模式和关系
  • 凭证状态表可以有多个具有相同凭证号的条目。因为每次发送凭证以进行验证时都必须记录...所以可能会有凭证001、拒绝和数据。然后后来的凭证001,接受,然后是一个日期。在这种情况下,连接仍然有效吗?

标签: php mysql loops


【解决方案1】:

好的,所以我使用了如下的加入建议:

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"

然后将该优惠券的最新互动置于顶部。 感谢大家的帮助!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-04-01
    • 2015-10-28
    • 1970-01-01
    • 1970-01-01
    • 2015-09-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多