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