English 中文(简体)
外交部
原标题:Codeigntier Active Record Join

在我试图组建一个正义与平等联合组织时,诽谤者的积极记录似乎并没有打上我的所有力量。

DB 设置(简化)

Things                                        Likes
----------------------------------------     ---------------------------
|  thing_id  |   thing_name |  user_id |     | thing_id   |   user_id  |
----------------------------------------     ---------------------------
|     1      |    Thing 1   |   5555   |     |    2       |    6666    |
|     2      |    Thing 2   |   5555   |     |    3       |    7777    |
|     3      |    Thing 3   |   5555   |     ---------------------------
|     4      |    Thing 4   |   5555   |
----------------------------------------    

当我用用户填写清单时,我收到4份预期结果。

  $sql = $this->db->where( things.user_id , $user_id)->get( things )->result();

但是,当我这样做时,我会收到3份(第2、第3和第4页)。

  $sql = $this->db->select( things.thing_id as thing_id, COUNT(likes.thing_id) as likes )
                    ->where("things.user_id", $user_id)
                    ->from( things )
                    ->join( likes ,  things.thing_id = likes.thing_id ,  LEFT )
                    ->group_by( likes.thing_id )
                    ->get()
                    ->result();

我困惑不解,因为它不象表上那样,但不是表上的东西,因此,如果是JOIN失败,我就感到困惑。

Solved: By switching GROUP BY from likes.thing_id to things.thing_id

最佳回答

Solved by switching GROUP BY from likes.thing_id to things.thing_id.

我的3个项目是作为<代码”分类的结果。 Thing 2, Thing 3 and NUL, which two items not in the liess 表格的分类如下。

问题回答

暂无回答




相关问题
Codeigniter WHERE on "AS" field

I have a query where I need to modify the selected data and I want to limit my results of that data. For instance: SELECT table_id, radians( 25 ) AS rad FROM test_table WHERE rad < 5 ORDER BY rad ...

Problem find joined table in rails

I have model represent association rule (Body => Head) def Item has_many :heads has_many :bodies ... end def Rule has_many :heads has_many :bodies ... end def Body belongs_to :item belongs_to :rule ...

FreeTDS Bad token from the server (SQL Server)

Today we had a lot more activity than normal between our Ruby on Rails application and our remote legacy SQL Server 2005 database, and we started getting the error below intermittently. What is is? ...

Castle ActiveRecord: one-to-one

While playing around with one-to-one associations in castle activerecord I stumbled upon the following problem: I m trying to model a one-to-one relationship (user-userprofile in this case). I ...

Sending email updates: model or observer?

I have an Event model, which stores an event feed for each user. I also need to email the updates to users which have enabled email notifications in their profile. From an architectural point of view,...

activerecord has_many :through find with one sql call

I have a these 3 models: class User < ActiveRecord::Base has_many :permissions, :dependent => :destroy has_many :roles, :through => :permissions end class Permission < ActiveRecord::...

热门标签