php_guest Posted March 31, 2010 Share Posted March 31, 2010 I am not very experienced with MySql and I would like to optimized the below queries. Please could you tell me what method would you suggest (join, union or other?). Is possible to make for all that only 1 query? TABLES: friends who | whom | acceptedstatus companies companyid | companyname | url | companylogo employees id | companyid | userid I have a page where I need to display to user all companies where user friends are employed. I have now the below code, but I believe it could be better optimized: //$selfid is user id from session $result=mysql_query("SELECT whom FROM friends WHERE who='$selfid'"); while($row=mysql_fetch_assoc($result){ $result2=mysql_query("SELECT companyid FROM employees WHERE userid='$row[whom]'"); while($row2=mysql_fetch_assoc($result2){ $result3=mysql_query("SELECT companyname, companylogo, url FROM companies WHERE companyid='$row2[companyid]'"); $row3=mysql_fetch_assoc($result3); $companyname[]=$row3[companyname]; $compnylogo[]=$row3[companylogo]; $companyurl[]=$row3[url]; } } Link to comment https://forums.phpfreaks.com/topic/197170-how-to-optimize-this-query/ Share on other sites More sharing options...
zeodragonzord Posted March 31, 2010 Share Posted March 31, 2010 Try this... SELECT *.companies FROM friends INNER JOIN employees ON friends.whom = employees.userid INNER JOIN companies ON employees.companyid = companies.companyid; Link to comment https://forums.phpfreaks.com/topic/197170-how-to-optimize-this-query/#findComment-1034971 Share on other sites More sharing options...
php_guest Posted April 1, 2010 Author Share Posted April 1, 2010 Thank you! I found also two another options. But now I am confused what is the best option. a) SELECT companyname,companylogo,url FROM friends,employees,companies WHERE who='$selfid' AND userid=whom AND companies.companyid=employees.companyid b) SELECT companyname,companylogo,url FROM companies WHERE companyid = (SELECT companyid FROM employees WHERE userid = (SELECT whom FROM friends WHERE who ='$selfid') or c) SELECT companyname, companylogo, url FROM companies INNER JOIN employees ON employees.companyid = companies.companuid -- which have employess INNER JOIN friends ON friends.whom = employees AND friends.who='$selfid' Link to comment https://forums.phpfreaks.com/topic/197170-how-to-optimize-this-query/#findComment-1035179 Share on other sites More sharing options...
ignace Posted April 1, 2010 Share Posted April 1, 2010 A and C are equal, B is possible but not really the optimized solution you are looking for. Link to comment https://forums.phpfreaks.com/topic/197170-how-to-optimize-this-query/#findComment-1035181 Share on other sites More sharing options...
php_guest Posted April 14, 2010 Author Share Posted April 14, 2010 I have now another but similar problem. The situation is very similar but I need to get information from two tables. Is also for this any similar solution or do I need now to do actually a new query for each user? table employees id | userid | companyid | position table users userid | firstname | lastname | age | email | password ... $employees=mysql_query("SELECT userid, [color=red]position[/color] FROM employees WHERE companyid='$companyid' LIMIT $paginationLimit "); while($rowemployees=mysql_fetch_assoc($employees){ $userid=$rowemployees[userid]; $position[]=$rowemployees[position]; $employeeDetails=mysql_query("SELECT * FROM users WHERE userid='$userid'"); $rowEmployeeDetails=mysql_fetch_assoc($employeeDetails) $firstname[]=$rowEmployeeDetails[firstname]; $lastname[]=$rowEmployeeDetails[lastname]; $age[]=$rowEmployeeDetails[age]; $position[]=$rowEmployeeDetails[position]; } Thanks again! Link to comment https://forums.phpfreaks.com/topic/197170-how-to-optimize-this-query/#findComment-1041972 Share on other sites More sharing options...
Ken2k7 Posted April 14, 2010 Share Posted April 14, 2010 Thank you! I found also two another options. But now I am confused what is the best option. a) SELECT companyname,companylogo,url FROM friends,employees,companies WHERE who='$selfid' AND userid=whom AND companies.companyid=employees.companyid b) SELECT companyname,companylogo,url FROM companies WHERE companyid = (SELECT companyid FROM employees WHERE userid = (SELECT whom FROM friends WHERE who ='$selfid') or c) SELECT companyname, companylogo, url FROM companies INNER JOIN employees ON employees.companyid = companies.companuid -- which have employess INNER JOIN friends ON friends.whom = employees AND friends.who='$selfid' C. IIRC, A is slower than C. And B has syntax errors. Link to comment https://forums.phpfreaks.com/topic/197170-how-to-optimize-this-query/#findComment-1042009 Share on other sites More sharing options...
fenway Posted April 17, 2010 Share Posted April 17, 2010 A and C are identical. Link to comment https://forums.phpfreaks.com/topic/197170-how-to-optimize-this-query/#findComment-1043722 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.