dongshao1021 2015-05-20 12:37
浏览 23

codeigniter:连接中第二个表的单行

I have two tables : user and payment.

The user table have 3 records and each has 2 payment records in payment table. I want the last payment details of the users, but instead of getting 3 rows, I get only 1 row.

$this->db->select('user.*, max(payment.create_at) as last_payment_at');
$this->db->from('user');
$this->db->join('payment','payment.user_id=user.id', 'left');
$this->db->order_by("user.id", "desc"); 
return $this->db->get()->result_object();
  • 写回答

1条回答 默认 最新

  • dongya9346 2015-05-20 12:43
    关注
    $this->db->select('user.*, max(payment.create_at) as last_payment_at'); // you do not have to use max(payment.create_at) as last_payment_at if you are using desc and limit 1
    $this->db->from('user');
    $this->db->join('payment','payment.user_id=user.id', 'left');
    $this->db->order_by("user.id", "desc"); // you can omit this line if you are getting max value
    
    $this->db->limit(1);  // <======= add limit for 1
    
    return $this->db->get()->result_object();
    
    评论

报告相同问题?

悬赏问题

  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
  • ¥15 关于#hadoop#的问题
  • ¥15 (标签-Python|关键词-socket)
  • ¥15 keil里为什么main.c定义的函数在it.c调用不了
  • ¥50 切换TabTip键盘的输入法
  • ¥15 可否在不同线程中调用封装数据库操作的类
  • ¥15 微带串馈天线阵列每个阵元宽度计算