简体   繁体   English

Codeigniter联接,分组并从一个表计数行

[英]Codeigniter join, group by and count a row from one table

I'm struggling a bit with this code to get the count result from book_listing (totally) which is the total count of bookings. 我在这段代码中苦苦挣扎,以从book_listing(全部)获得计数结果,这是预订总数。 From the result, some are correct but some are multiplied by 3 or so. 从结果来看,有些是正确的,但有些乘以3左右。

Here is my code: 这是我的代码:

$this->db->where('list_status', 1)
         ->where('list_date >=', date('Y-m-d'))
         ->order_by("user_listings.list_created", "desc")
         ->limit(18, null)
         ->select('*, COUNT(user_bookings.book_listing) as totally')
         ->from('user_listings')
         ->join('user_images', 'user_images.img_listing = user_listings.list_reference')
         ->join('user_states', 'user_states.state_id = user_listings.list_state')
         ->join('user_bookings', 'user_bookings.book_listing = user_listings.list_reference', 'left')
         ->group_by('user_listings.list_reference')
         ->get();

Thanks to any help :) 感谢任何帮助:)

So I solved it, the user_images table seems to be causing the issue due to multiple images being fetched while I need it to return only one. 所以我解决了这个问题,由于要获取多个图像,而只需要返回一个图像,因此user_images表似乎引起了问题。

$this->db->where('list_status', 1)
         ->where('list_date >=', date('Y-m-d'))
         ->order_by("user_listings.list_created", "desc")
         ->limit(18, null)
         ->select('user_listings.*,image.*,user_states.*')
         ->select('COUNT(user_bookings.book_listing) as totalBooked')
         ->from('user_listings')
         ->join('user_bookings', 'user_bookings.book_listing = user_listings.list_reference', 'left')
         ->join('(SELECT * FROM user_images WHERE user_images.img_key IN(SELECT MIN(user_images.img_key) FROM user_images GROUP BY user_images.img_listing)) AS image',
                                            'image.img_listing = user_listings.list_reference')
         ->join('user_states', 'user_states.state_id = user_listings.list_state')
         ->group_by('user_listings.list_reference')
         ->get();

Basically, I had to do a select from the join table for user_images . 基本上,我必须从连接表中为user_images Hope it helps someone else :) 希望它可以帮助其他人:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Codeigniter 3-从一个表计数行并联接 - Codeigniter 3 - Count rows from one table and join 如何从右侧表中加入每个组的行数 - How to join row count of each group from right side table mysql使用组从一个表中计算行并从第二个表中加入 - mysql count rows from one table using group and join from 2nd table 来自同一表的GROUP BY和LEFT JOIN以及COUNT个 - GROUP BY and LEFT JOIN with COUNT from same table PHP Codeigniter MySQL 查询 - 将 4 个表连接在一起,其中 3 个表使用每个表中的一列分组 - PHP Codeigniter MySQL query - join 4 tables together, with 3 tables using group by one column from each table Codeigniter:一个表中有多个COUNT和SUM - Codeigniter: multiple COUNT and SUM from one table 使用Codeigniter将表中的一行与MySQL中其他两个表的多行联接 - Join one row from a table with multiple rows of two other tables in MySQL using Codeigniter codeigniter join 计算另一个表 - codeigniter join count another table 来自JOIN子句和GROUP_CONCAT的MySQL LIKE从连接表中仅返回一行 - MySQL LIKE from JOIN clause and GROUP_CONCAT returns only one row from joined table 联接其他表,并在单个列中对相同的ID进行分组 - Join other table and count the same id with group by in one single column
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM