【问题标题】:Mysqli select query based on two different fieldsMysqli select查询基于两个不同的字段
【发布时间】:2016-04-25 10:40:19
【问题描述】:

这是我的表结构
表:销售额

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
}

在此表中有两个字段,一个是 tax,另一个是 total_tax,tax 字段用于存储项目税,total_tax 用于存储总计发票中的税。
如果一个值存储在税字段 (!=0) 中,则两个字段的初始默认值为零tax 和 total_tax 字段,因此其默认值为零。
我想搜索 Zero Tax 项结合 tax 和 total_tax 字段
如何搜索tax=0和total_tax=0(避税>0和total_tax>0)

【问题讨论】:

  • 但是表架构中没有 returnd 字段那么为什么要在查询中添加它?
  • 共享表格字段数据类型;
  • 数量:双倍,单位价格:双倍,折扣:双倍,税:双倍,总税:双倍,日期:日期
  • 检查您从中获取并存在于您的数据库中的数据表单;
  • 我搜索 Tax 和 total_tax 为零它返回两个日期之间我的数据库中的所有数据,但我搜索 tax =5 它返回正确答案。零是问题

标签: php mysql


【解决方案1】:

试试看

    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
    }

【讨论】:

  • 先在 phymyadmin 中尝试 sql,然后在 php 代码中应用该查询
【解决方案2】:

我找到了解决办法..

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

【讨论】:

    猜你喜欢
    • 2020-10-01
    • 1970-01-01
    • 1970-01-01
    • 2011-12-05
    • 1970-01-01
    • 2020-08-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多