CodeIgniter Forums
Fetching first 5 values against every value in another column in another table - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forum-5.html)
+--- Forum: Best Practices (https://forum.codeigniter.com/forum-12.html)
+--- Thread: Fetching first 5 values against every value in another column in another table (/thread-68553.html)



Fetching first 5 values against every value in another column in another table - anirudh88a - 07-26-2017

Hi guys, hope this is the right section for this question.
I have 2 tables, topics (topic_id - pk) and comments (comment_id - pk, topic_id - fk). For all the topics, I want to get first 5 comment IDs in one go. Can you give me insights into how to go about it please?


RE: Fetching first 5 values against every value in another column in another table - Martin7483 - 07-26-2017

Use a join and limit the select to 5


RE: Fetching first 5 values against every value in another column in another table - ciadvantage - 07-26-2017

also you can add 'order by comment_id ASC' (i.e ascending order of comments)


RE: Fetching first 5 values against every value in another column in another table - skunkbad - 07-26-2017

You answer may be using a combination of GROUP_CONCAT and FIND_IN_SET:

https://stackoverflow.com/questions/2129693/using-limit-within-group-by-to-get-n-results-per-group


RE: Fetching first 5 values against every value in another column in another table - anirudh88a - 07-26-2017

(07-26-2017, 05:30 PM)skunkbad Wrote: You answer may be using a combination of GROUP_CONCAT and FIND_IN_SET:

https://stackoverflow.com/questions/2129693/using-limit-within-group-by-to-get-n-results-per-group

Thanks! Will try this out