![]() |
Query Advice RESOLVED - Printable Version +- CodeIgniter Forums (https://forum.codeigniter.com) +-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20) +--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23) +--- Thread: Query Advice RESOLVED (/showthread.php?tid=7636) |
Query Advice RESOLVED - El Forum - 04-17-2008 [eluser]OES[/eluser] Hi Peeps Would like some advice on a query or a better way to do it. I have a simple news table and second with comments ie. Code: Table news If I done a query to loop the latest 10 news items what would be the bast way to count how many comments are associated to them. ie Comment Title (2 Comments) Or would it be best to include in the news table a line for comments and increase the number when someone submits a comment. Sorry but a newbie to this :-). Hope you can advise on the best way. Thanks in advanced Query Advice RESOLVED - El Forum - 04-17-2008 [eluser]webthink[/eluser] two ways to do it: You could grab a your list of news items with one query and then for each one do a query to count comments. That's 11 round trips to the database. In a single query you could do something like this SELECT news.news_id, COUNT(news_comments.newscomment_id) FROM news, news_comments WHERE news.news_id = news_comments.newscomment_news_id GROUP BY news.news_id Query Advice RESOLVED - El Forum - 04-17-2008 [eluser]OES[/eluser] Hi Webthink. Thanks for the respone I managed to scratch my head for a while but got it in the end. I done this query. Code: SELECT And works perfect. Now just to put it into CI. that should be a laugh. Thanks Query Advice RESOLVED - El Forum - 04-17-2008 [eluser]OES[/eluser] Ok I think I converted it to an Active Record Query but its obviously wrong. Code: $this->db->select("ci_newsentries.*, COUNT(ci_newscomments.newscomment_id) AS no_comments"); but just trying to print the array its obvioulsy not working. Is the a way to echo the SQL statement to see whats going on ?? Hope you can help Query Advice RESOLVED - El Forum - 04-17-2008 [eluser]xwero[/eluser] $this->db->last_query() Query Advice RESOLVED - El Forum - 04-17-2008 [eluser]OES[/eluser] Thanks for that. My query is perfect from what I can see and it works in MYSQL. so would this be wrong $news_tiems = $query->row_array(); ? Get this then Im on a roll :-) Thanks Query Advice RESOLVED - El Forum - 04-17-2008 [eluser]OES[/eluser] Ok I was being thick All I needed was. $data['news'] = $query->result_array(); Thanks for the help guys. |