简体   繁体   中英

Best practise to search in MySQL result

Good day everyone. I am trying to find out what is the best way to search inside a select statement. I currently have the following select statement:

$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%'";

This returns all the voucher numbers linked to the grn searched. They now want to have displayed the vouchers's status which is in another table.

I was thinking of putting another mysql query in the while loop:

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>";
            }

Is this the right way to do it or is there better way that is easier on the database or server?

Thank you so much

Ok so I used the join suggestion as follows:

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"

Which then places the most recent interaction of that voucher at the top. Thank for all the help!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM