dsvtnz6350 2013-11-23 14:40
浏览 40
已采纳

我需要CodeIgniter的帮助,从数据库中获取数据

I have 3 table : users , posts , category

category : id , name

1 linux
2 Mac

users : id , name , email , image

1  Jak    t@t.com    afs
2  Stif   t2@t.com    122afs

posts : id , idc , idu , title , content

If I use getall function

function getall(){
   $this->db->limit(10);
   $this->db->order_by("id", "desc"); 
   $query = $this->db->get('posts');
   $res=$query->result();
   return $query->result();
   }

then result like this

 - 1  1  2  test   content1
 - 2  1  1  test2   content2

but I need get result like this :

 - 1  linux  Stif   test   content1
 - 2  linux  Jak    test2   content2

How do I do that?

  • 写回答

1条回答 默认 最新

  • dps43633 2013-11-23 14:46
    关注

    with join you can make the query that you want try this:

    function getall(){
       $this->db->select('*, category.id as category_id, users.id as user_id');
       $this->db->from("posts");
       $this->db->join("users", "users.id = posts.idu");
       $this->db->join("category", "category.id = posts.idc"); 
       $this->db->order_by("posts.id", "desc"); 
       $this->db->limit(10);
       $query = $this->db->get();
       $res=$query->result();
       return $query->result();
    

    }

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站
  • ¥50 成都蓉城足球俱乐部小程序抢票
  • ¥15 yolov7训练自己的数据集
  • ¥15 esp8266与51单片机连接问题(标签-单片机|关键词-串口)(相关搜索:51单片机|单片机|测试代码)
  • ¥15 电力市场出清matlab yalmip kkt 双层优化问题
  • ¥30 ros小车路径规划实现不了,如何解决?(操作系统-ubuntu)