I would like to merge these 2 queries to retrieve my datas in a view :
QUERY 1 :
$query = $this->db->query('SELECT ft_upload_data.*, ft_categories.*, ft_categories.category_name
FROM ft_upload_data
LEFT JOIN assigned_categories ON assigned_categories.ft_upload_data_id = ft_upload_data.post_id
LEFT JOIN ft_categories ON ft_categories.cat_id = assigned_categories.ft_categories_id
ORDER BY ft_upload_data.rank ASC
');
return $query->result();
QUERY 2 :
$query2 = $this->db->query('SELECT a.post_id,
COUNT(*) AS num_comments
FROM ft_upload_data a
JOIN ft_comments c ON c.post_id = a.post_id
GROUP BY a.post_id');
return $query2->result();
I can’t figure it out :/ Any ideas to trick this?
Thanks !
The simplest (but not necessarily the most efficient) way would be to do it in an in-line query:
A more efficient way would be to join to the ft_comments table and group by post_id (assuming that this uniquely identifies ft_upload_data rows), like so: