如何使用两个查询结果仅显示一次数据

时间:2018-11-14 10:39:59

标签: php mysql codeigniter

如何避免在视图页面上重复打印数据。.我在phpmyadmin中尝试了此查询,但显示了正确的结果。.但是我多次打印数据。.如何避免这种情况。 $this->db->distinct(); $this->db->group_by(vno).. 但是我没有得到预期的结果。帮助我实现这一点。这是我尝试过的代码:

控制器代码:

$this->db->where('billdate >=', $newDate);
$this->db->where('billdate <=', $newDate2);
$this->db->select('');
$this->db->from('purchaseitem');
$this->db->distinct();
//$this->db->join('purchasebill', 'purchasebill.no = purchaseitem.billno','left outer');
$this->db->join('itemmaster','itemmaster.itcode = purchaseitem.Product_Code','left outer');
$query = $this->db->get('')->result_array();
$data['query'] = $query;
$this->db->where('date >=', $newDate);
$this->db->where('date <=', $newDate2);
$this->db->select();
$this->db->from('purchasebill');
$this->db->order_by('voucherno');
$this->db->join('parmaster','parmaster.Pcode = purchasebill.partyname','left outer');
$query = $this->db->get('')->result_array();
$data['query1'] = $query;

$this->load->view('Receipt_View1', $data);

My Current output

查看代码:

<?php $rowcount = 1 ?>                          
<?php foreach ($query1 as $row): ?>

        <tr>
            <td><?=$rowcount;?></td>
            <td><?=$row['voucherno'];?></td>
            <td><?=$row['date'];?></td>
            <td><?=$row['PName'];?></td>
            <?php $rowcount +=1?>
            <?php foreach ($query as $row): ?>
                <tr>
            <td><?=$row['itemname'];?></td>
            <td><?=$row['Qty'];?></td>
            <td><?=$row['bundle'];?></td>

            <?php endforeach ?> 
            <?php endforeach ?> 
        </tr></tr>
        <?php $total_qty=0; ?>  
    <?php foreach($query as $row): ?>
            <?php $total_qty += $row['Qty'];?>
        <?php endforeach ?>
        <?php $total_bundle=0; ?>   
    <?php foreach($query as $row): ?>
            <?php $total_bundle += $row['bundle'];?>
        <?php endforeach ?>

问题出在purchaseitem表上,因为根据凭证没有它必须在表上打印条目,但是它正在打印从日期到日期的所有可用数据。我尝试通过放置where条件但它是结果表格中没有任何内容。.关于如何指定凭证编号的任何想法。这是我的代码$this->db->where('vocuerno','vno。..我在采购项目和采购帐单上都没有凭证

1 个答案:

答案 0 :(得分:0)

您需要在第二个查询中而不是在第一部分中使用GROUP_BY。

尝试在此处添加它,从控制器代码的第10行开始显示

....

$this->db->where('date >=', $newDate);
$this->db->where('date <=', $newDate2);
$this->db->from('purchasebill');
$this->db->order_by('voucherno');
$this->db->group_by('voucherno'); // <- HERE
$this->db->join('parmaster','parmaster.Pcode = purchasebill.partyname','left outer');
$query = $this->db->get('')->result_array();
$data['query1'] = $query;