dri8163 2014-08-14 08:48
浏览 93

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

i want to get records from both users and timerecord table but timerecord shows multiple rows because of duplicated id, and now i used group_by to limit the row in timerecord however the record is not showing the latest record i also used orderby but no luck, please someone help me, thanks!

  • 写回答

4条回答 默认 最新

  • duanqun7761 2014-08-14 10:21
    关注

    can you try this?

    $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');
    
    评论

报告相同问题?

悬赏问题

  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)