mysql,codeigniter活动记录类

时间:2012-09-30 06:28:33

标签: codeigniter

表1

house_id|house_name|status

  1     |     A    |  1
  2     |     B    |  1
  3     |     C    |  0
  4     |     D    |  1
  5     |     E    |  1
  6     |     F    |  1

表1

house_id| image

   2    | img1.jpg
   2    | img2.jpg
   1    | img3.jpg
   4    | img4.jpg
   1    | img5.jpg
   4    | img6.jpg
   3    | img7.jpg

我想从table1中选择所有house_id,其中status = 1(降序),从table2中为table1的每个house_id选择不同的图像。

最后输出显示如下:

输出

house_id|house_name|image

   6    |    F     |NULL
   5    |    E     |NULL
   4    |    D     |img4.jpg
   2    |    B     |img1.jpg  
   1    |    A     |img3.jpg  

请帮我用普通的mysql或CI活动记录类方法代码..

1 个答案:

答案 0 :(得分:3)

试试这个....(普通的mysql)

SELECT t1.*,t2.image FROM table1  t1
LEFT JOIN table2 t2 on t1.house_id=t2.house_id
WHERE t1.status= 1 GROUP BY t1.house_id ORDER BY house_id desc

活跃记录......

$this->db->select(t1.*,t2.image);
$this->db->from('table1'.' t1');
$this->db->join('table2'.' t2','t1.house_id=t2.house_id','left');
$this->db->where('t1.status',1);
$this->db->group_by("t1.house_id"); 
$this->db->order_by("house_id");
相关问题