Codeigniter计数来自连接表

时间:2015-05-24 11:31:38

标签: php mysql sql codeigniter activerecord

我正在使用codeigniter进行在线讨论,我需要获得所有主题及其所有数据的结果,例如帖子后ID,帖子后发布,文本等,还要计算对该特定主题的回复。

05-25 11:26:24.184: E/AndroidRuntime(1120): FATAL EXCEPTION: main
05-25 11:26:24.184: E/AndroidRuntime    (1120):java.lang.RuntimeException:Unable to start activity ComponentInfo     {com.jan.energyservice/com.jan.energyservice.Nieuw_huis1}:     java.lang.NullPointerException
05-25 11:26:24.184: E/AndroidRuntime(1120):     at  android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
05-25 11:26:24.184: E/AndroidRuntime(1120):     at     android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
05-25 11:26:24.184: E/AndroidRuntime(1120):     at android.app.ActivityThread.access$600(ActivityThread.java:141)
05-25 11:26:24.184: E/AndroidRuntime(1120):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
05-25 11:26:24.184: E/AndroidRuntime(1120):     at android.os.Handler.dispatchMessage(Handler.java:99)
05-25 11:26:24.184: E/AndroidRuntime(1120):     at android.os.Looper.loop(Looper.java:137)
05-25 11:26:24.184: E/AndroidRuntime(1120):     at android.app.ActivityThread.main(ActivityThread.java:5041)
05-25 11:26:24.184: E/AndroidRuntime(1120):     at java.lang.reflect.Method.invokeNative(Native Method)
05-25 11:26:24.184: E/AndroidRuntime(1120):     at java.lang.reflect.Method.invoke(Method.java:511)
05-25 11:26:24.184: E/AndroidRuntime(1120):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
05-25 11:26:24.184: E/AndroidRuntime(1120):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
05-25 11:26:24.184: E/AndroidRuntime(1120):     at dalvik.system.NativeStart.main(Native Method)
05-25 11:26:24.184: E/AndroidRuntime(1120): Caused by: java.lang.NullPointerException
05-25 11:26:24.184: E/AndroidRuntime(1120):     at com.jan.energyservice.Nieuw_huis1.setupVolgendePagina(Nieuw_huis1.java:29)
05-25 11:26:24.184: E/AndroidRuntime(1120):     at com.jan.energyservice.Nieuw_huis1.onCreate(Nieuw_huis1.java:20)
05-25 11:26:24.184: E/AndroidRuntime(1120):     at android.app.Activity.performCreate(Activity.java:5104)
05-25 11:26:24.184: E/AndroidRuntime(1120):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
05-25 11:26:24.184: E/AndroidRuntime(1120):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
05-25 11:26:24.184: E/AndroidRuntime(1120):     ... 11 more

这是我当前的“数据库选择器”。我的另一个表叫做cols响应:response_id,forum_id,author等。如何在那里加入?但只是结果的数量?喜欢:对于这个主题,有0个答案,对于这个答案,有3个答案,不需要答案的数据,只有关于主题和答案数量的数据。但我需要坚持下去:

$this->db->where('language', $lang)
         ->order_by('forum_id', 'desc')->limit($limit, $offset)
         ->get('forum');

由于分页和多语言支持。感谢。

1 个答案:

答案 0 :(得分:0)

谢谢大家,这是工作代码:

$this->db->where('forum.language', $lang)
         ->order_by('forum.forum_id', 'desc')
         ->limit($limit, $offset)
         ->select('forum.*, COUNT(responses.response_id) as num_answers')
         ->from('forum')
         ->join('responses', 'responses.forum_id = forum.forum_id','left')
         ->group_by('forum_id')
         ->get();