【问题标题】:mysql , codeigniter active record class [closed]mysql,codeigniter活动记录类[关闭]
【发布时间】:2012-09-30 06:28:32
【问题描述】:

table1

house_id|house_name|status

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

table1

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 (descending) ,从 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活动记录类方法编码..

【问题讨论】:

  • 我不明白你的问题,你想要代码吗?因为这似乎很容易使用左连接,并且您可以使用活动记录轻松做到这一点。
  • 是的,我想要代码。@Jonathan Chow
  • 您确实意识到这是一个问答网站,而不是“为我工作”网站,对吧?
  • 谢谢link

标签: codeigniter


【解决方案1】:

试试这个......(普通的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");

【讨论】:

  • 是的....已编辑.. :) :)..谢谢...
  • ** 先生,您的答案与我给定的输出不匹配(放弃页面)。如果我生成您的 mysql 代码,则缺少 (6 | F |NULL)。 . . .- @bipen
  • 在这种情况下.. 然后更改此行 $this->db->group_by("t2.house_id"); to.. $this->db->group_by("t1.house_id");
  • 更新了答案..请检查... :)
  • 是的!那行得通!谢谢你!
猜你喜欢
  • 1970-01-01
  • 2011-02-28
  • 1970-01-01
  • 2012-01-23
  • 1970-01-01
  • 2012-06-14
  • 1970-01-01
  • 2011-02-22
  • 2011-10-22
相关资源
最近更新 更多