Active records - "smart" select - 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: Active records - "smart" select (/showthread.php?tid=52554) Pages:
1
2
|
Active records - "smart" select - El Forum - 06-15-2012 [eluser]ibnclaudius[/eluser] I have three tables: users(id,name), users_communities(user_id,community_id), communities(id,name) I know how to select the communities the user doesn't have, but I want a more efficient result... Here's my idea, I'll show an example. User A has these communities: PHP,JAVA,ORACLE User B: PHP,MYSQL,JAVASCRIPT User C: PHP,CODEIGNITER,SOCCER User D: PHP,CODEIGNITER,MYSQL Let's say I want to show to user A the top 2 communities he may want. In this case would be CODEIGNITER and MYSQL. What I did? Select all common users in communities that I have. Select the 2 most common communities in these users. I'm pretty sure that should be something much more complex, but this is the beginning... Active records - "smart" select - El Forum - 06-15-2012 [eluser]vitoco[/eluser] I don't use active records, so i can only help you with plain sql : Code: $user_id = 1 ; Saludos Active records - "smart" select - El Forum - 06-15-2012 [eluser]ibnclaudius[/eluser] I got it: http://sqlfiddle.com/#!2/c501e/47 But there's still a little problem, this return only communities that have a user that follow another community in common with me. I want to select all communities, but ordering than in a smart way, just like the sqlfiddle link... What should I change? Active records - "smart" select - El Forum - 06-15-2012 [eluser]vitoco[/eluser] i get nothing from the sqlfiddle link Active records - "smart" select - El Forum - 06-15-2012 [eluser]ibnclaudius[/eluser] http://sqlfiddle.com/#!2/c501e/41 Active records - "smart" select - El Forum - 06-15-2012 [eluser]ibnclaudius[/eluser] Code: SELECT communities.* Active records - "smart" select - El Forum - 06-15-2012 [eluser]vitoco[/eluser] I had to use a sub-select ( very fast if the table is well indexed ) but it works. http://sqlfiddle.com/#!2/444e5/12/0 Saludos Active records - "smart" select - El Forum - 06-15-2012 [eluser]ibnclaudius[/eluser] Get nothing from the link... =/ Active records - "smart" select - El Forum - 06-15-2012 [eluser]vitoco[/eluser] Code: // http://sqlfiddle.com/#!2/444e5/12 Saludos Active records - "smart" select - El Forum - 06-15-2012 [eluser]ibnclaudius[/eluser] Thanks, is it possible to make it using join? |