Mysqli基于两个不同的字段

时间:2016-04-25 10:40:20

标签: php mysql

这是我的桌子结构
表:销售

invoice_no  prd_code    quantity    unit_price  discount    tax total_tax   date    delet
   135          1          1            120         0        5      0     1/8/2016      1
   135          1          1            120         0        5      0     1/8/2016      0
   135          2          3            30          0        0      0     1/8/2016      0
   135          3          1            165         0        5      0     1/8/2016      0
   136          2          4            30          3        5      0     1/16/2016     0
   136          1          2            120         0        0      5     1/16/2016     0
   136          1          2            120         0        0      5     1/16/2016     1
   136          1          2            120         0        0      5     1/16/2016     1
   137          1          2            120         0        0      0     1/15/2016     0
   137          1          2            120         0        0      0     1/15/2016     0
   138          2          12           30          0        0      6     1/16/2016     0
   138          3          10           165         0        0      6     1/16/2016     0

这是我的Html代码

<input type = "date" id = "fmdte" name = "fmdte" class = "form-control" />
<input type = "date" id = "todate" name = "todate" class = "form-control" />
<input type = "text" id = "tax" name = "tax" class = "form-control" />
<input type = "submit" id = "ser" name = "ser" value = "Search" />

这是我的PHP代码:

if (isset($_POST['ser']))
{
   $fmdt = $_POST['fmdte'];
   $todt = $_POST['todate'];
   $tax_ser = $_POST['tax'];
   $purqry = $db->execute("select * from sales where date BETWEEN '$fmdt' and '$todt' and (tax='$tax_ser' OR total_tax='$tax_ser') and delet='0'"); //,order_no
}

在此表中有两个字段,一个是,另一个是 total_tax ,税字段用于存储商品税,而total_tax用于存储总额发票上的税 如果税收字段中的值存储(!= 0)total_tax值存储为零,则最初两个字段的默认值为零,如果total_tax字段中的值存储(!= 0)税收字段值存储为零某些时间没有存储的值 tax和total_tax 字段,因此其默认值为零。
我想搜索零税项目合并tax和total_tax字段
如何搜索tax = 0和total_tax = 0 (避免税&gt; 0和total_tax&gt; 0)

2 个答案:

答案 0 :(得分:0)

试试吧

    if (isset($_POST['ser']))
    {
         $fmdt = $_POST['fmdte'];
         $todt = $_POST['todate'];
         $tax_ser = $_POST['tax'];

         //use concat when passing variable value
         $purqry = $db->execute("select * from sales ".
         "where (date BETWEEN '". $fmdt ."' and '". $todt ."') ".
         "and (tax= ". $tax_ser ." OR total_tax= ". $tax_ser .") and delet='0' "); //,order_no
    }

答案 1 :(得分:0)

我找到了解决方案..

if (isset($_POST['ser'])) {
    $fmdt = $_POST['fmdte'];
    $todt = $_POST['todate'];
    $tax_ser = $_POST['tax'];
    if ($tax_ser == '0') {
        $purqry = $db->execute("select * from sales where date BETWEEN '$fmdt' and '$todt' and (tax='$tax_ser' and total_tax='$tax_ser') and delet='0'");
    }
    else {
        $purqry = $db->execute("select * from sales where date BETWEEN '$fmdt' and '$todt' and (tax='$tax_ser' OR total_tax='$tax_ser') and delet='0'");
    }
}