Codeigniter:分组并从连接表中获取最新记录

时间:2014-08-14 08:48:33

标签: php mysql codeigniter

$this -> db -> select('users.*');
$this -> db -> select('timerecord.*');
$this -> db -> from('users');
$this -> db -> join('timerecord', 'users.id = timerecord.id');
//$this -> db -> where(array('users.id'=>$user_id), NULL, FALSE);
$this -> db -> where('users.status', 'Verified');
//$this -> db -> where('users.id', 'timerecord.id');
$this -> db -> order_by('timerecord.timeId', 'DESC');
$this -> db -> group_by('timerecord.id, users.id');

我想从两个用户和时间记录表中获取记录,但是时间记录由于重复的id而显示多行,现在我使用group_by来限制timerecord中的行但是记录没有显示最新记录我也使用了orderby但是没有好运,请有人帮助我,谢谢!

4 个答案:

答案 0 :(得分:1)

<div class="container">
    <script type="text/javascript">
        $(document).ready(function(){
            $('.approve').click(function(){
                return confirm("Verify this user?");
            });
            $('.delete').click(function(){
                return confirm("Are you sure you want to delete this account, everything will be deleted including its time record?");
            });
        }); 
    </script>
    <div class="empRec">
        <div class="empRec-content">
            <table class="tn">
              <tr>
                <th class="tn-4apw">Employee Name</th>
                <th class="tn-4apw">Date</th>
                <th class="tn-4apw">Day</th>
                <th class="tn-4apw">Time In/Out</th>
                <th class="tn-4apw"></th>
              </tr>
              <?php 
                foreach($getUsers as $row): 
                $timeStat = $row->status;
              ?>
              <tr>
                <td class="tn-ixdt"><?php if($timeStat == 'IN') { ?>
                    <img src="<?php echo base_url('assets/img/ico-online.png'); ?>" style="width:10px;">
                <?php } else { ?>
                    <img src="<?php echo base_url('assets/img/ico-offline.png'); ?>" style="width:10px;">           
                <?php } echo $row->name ?></td>
                <td class="tn-ixdt"><?php echo $row->dateToday ?></td>
                <td class="tn-ixdt"><?php echo $row->day ?></td>
                <td class="tn-ixdt"><?php echo $row->timeIn ?> - <?php echo $row->timeOut ?></td>
                <td class="tn-ixdt" style="text-align:right;"><button class="view" name="view"><img src="<?php echo base_url('assets/img/ico-view.png'); ?>"></img></button><button class="delete" name="delete"><img src="<?php echo base_url('assets/img/ico-delete.png'); ?>"></img></button></td>
              </tr>
              <?php endforeach; ?>
            </table>
        </div>
        <!-- Employee Record Content  -->
        <div class="empRec-bg"></div>
    </div>
    <!-- EMPLOYEE RECORD CONTAINER -->
</div>
<!-- CONTAINER -->

答案 1 :(得分:1)

$this->db->query("SELECT users.*, timerecord.* FROM (users) 
JOIN timerecord ON users.id = timerecord.id WHERE users.status = 'Verified'
group by users.id, timerecord.dateToday order by dateToday")->get()->result();

答案 2 :(得分:1)

$this->db->select('MAX(a.login_time) as login_time')

->from('bs_login_report a')

->group_by('a.user_id')

->get();

使用此 MAX(a.login_time)作为login_time ,您可以使用

实现最后一行

答案 3 :(得分:0)

你能试试吗?

$this -> db -> select('users.*');
$this -> db -> select('timerecord.*');
$this -> db -> from('users');
$this -> db -> join('timerecord', 'users.id = timerecord.timerecord.user_id');
//USE THE NAME OF THE ROW WHERE USER'S IDs ARE INSERTED ON YOUR TIMERECORD TABLE,
// THAT'S HOW YOU JOIN TABLE, RIGHT? 
// I ASSUME THAT YOU ARE JOINING USER'S ID AND TIMERECORD'S ID ON YOUR PREVIOUS POST //
//$this -> db -> where(array('users.id'=>$user_id), NULL, FALSE);
$this -> db -> where('users.status', 'Verified');
//$this -> db -> where('users.id', 'timerecord.id');
$this -> db -> order_by('timerecord.timeId', 'DESC');
$this -> db -> group_by('timerecord.id, users.id');