Pomoc - Szukaj - Użytkownicy - Kalendarz
Pełna wersja: [PHP][MYSQL]Optymalizacja zapytania
Forum PHP.pl > Forum > Przedszkole
reyne
witam, pobieram do tablic za pomocą tych trzech pętli i zapytań dane:

Kod
$result = dbquery("SELECT * FROM ".$db_prefix."groups ORDER BY group_id DESC LIMIT 5");
  while($data = dbarray($result)) {
       $result2 = dbquery("SELECT * FROM ".$db_prefix."groups_cats WHERE cat_id='".$data['group_cat']."'");
       while($catdata = dbarray($result2)) {
            $uresult = dbquery("SELECT * FROM ".$db_prefix."users WHERE user_id='".$data['group_admin']."'");
            while($udata = dbarray($uresult)) {


jak zrobić za pomocą tego jedno zapytanie?
domyślam się że chodzi o INNER JOIN aczkolwiek nie wiem jak tego użyć
wookieb
  1. SELECT a.*, b.* FROM groups AS a INNER JOIN groups_cats AS b ON (b.cat_id=a.group_cat) ORDER BY a.group_id


Tylko w miejscu a.*, b.* wybierz to co ci potrzebne
reyne
zrobiłem tak jak napisałeś:

Kod
$result = dbquery("
select a.group_cat, b.cat_id FROM ".$db_prefix."groups as a INNER JOIN ".$db_prefix."groups_cats as b ON (b.cat_id=a.group_cat) ORDER BY a.group_id  ");


niestety wyskakuje mi błąd:

Cytat
Nieznana kolumna 'a.group_cat' w field listNieznana kolumna 'a.group_cat' w field list
worriedsmiley.gif
wookieb
No to zamien to na kolumne ktora zawiera id kategorii.
reyne
tak się składa że to właśnie group_cat zawiera id kategorii w jakiej jest grupa
wookieb
Wyslij w języku sql backup struktury tych 3 tabel i paru przykładowych rekordów to ci zapodam zapytanie.
reyne
się robi !

  1. CREATE TABLE `ef_groups` (
  2. `group_id` tinyint(5) UNSIGNED NOT NULL AUTO_INCREMENT,
  3. `group_name` varchar(100) NOT NULL DEFAULT '',
  4. `group_description` text NOT NULL,
  5. `group_avatar` varchar(100) NOT NULL DEFAULT '',
  6. `group_admin` tinyint(3) NOT NULL DEFAULT '0',
  7. `group_cat` tinyint(1) NOT NULL DEFAULT '0',
  8. `group_time` int(10) NOT NULL DEFAULT '0',
  9. `group_forum` tinyint(1) NOT NULL DEFAULT '0',
  10. `group_access` tinyint(1) NOT NULL DEFAULT '0',
  11. `group_users` tinyint(3) UNSIGNED NOT NULL DEFAULT '0',
  12. PRIMARY KEY (`group_id`)
  13. ) TYPE=MyISAM AUTO_INCREMENT=2 ;
  14.  
  15. --
  16. -- Zrzut danych tabeli `ef_groups`
  17. --
  18.  
  19. INSERT INTO `ef_groups` VALUES (1, 'Maniacy..', 'Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.\r\n\r\nLorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.', '../../infusions/groups_system/img/avatars/avatar.jpg', 1, 1, 1218722762, 0, 101, 1);
  20.  
  21. -- --------------------------------------------------------
  22.  
  23. --
  24. -- Struktura tabeli dla `ef_groups_cats`
  25. --
  26.  
  27. CREATE TABLE `ef_groups_cats` (
  28. `cat_id` tinyint(5) UNSIGNED NOT NULL AUTO_INCREMENT,
  29. `cat_name` varchar(40) NOT NULL DEFAULT '',
  30. `cat_avatar` text NOT NULL,
  31. PRIMARY KEY (`cat_id`)
  32. ) TYPE=MyISAM AUTO_INCREMENT=2 ;
  33.  
  34. --
  35. -- Zrzut danych tabeli `ef_groups_cats`
  36. --
  37.  
  38. INSERT INTO `ef_groups_cats` VALUES (1, 'Ludzie', '');
  39.  
  40. -- --------------------------------------------------------
  41.  
  42.  
  43. CREATE TABLE `ef_users` (
  44. `user_id` smallint(5) UNSIGNED NOT NULL AUTO_INCREMENT,
  45. `user_name` varchar(30) NOT NULL DEFAULT '',
  46. `user_password` varchar(32) NOT NULL DEFAULT '',
  47. `user_email` varchar(100) NOT NULL DEFAULT '',
  48. `user_hide_email` tinyint(1) UNSIGNED NOT NULL DEFAULT '1',
  49. `user_location` varchar(50) NOT NULL DEFAULT '',
  50. `user_birthdate` date NOT NULL DEFAULT '0000-00-00',
  51. `user_aim` varchar(25) NOT NULL DEFAULT '',
  52. `user_icq` varchar(15) NOT NULL DEFAULT '',
  53. `user_msn` varchar(100) NOT NULL DEFAULT '',
  54. `user_yahoo` varchar(100) NOT NULL DEFAULT '',
  55. `user_web` varchar(200) NOT NULL DEFAULT '',
  56. `user_theme` varchar(100) NOT NULL DEFAULT 'Default',
  57. `user_offset` char(3) NOT NULL DEFAULT '0',
  58. `user_avatar` varchar(100) NOT NULL DEFAULT '',
  59. `user_sig` text NOT NULL,
  60. `user_posts` smallint(5) UNSIGNED NOT NULL DEFAULT '0',
  61. `user_joined` int(10) UNSIGNED NOT NULL DEFAULT '0',
  62. `user_lastvisit` int(10) UNSIGNED NOT NULL DEFAULT '0',
  63. `user_ip` varchar(20) NOT NULL DEFAULT '0.0.0.0',
  64. `user_rights` text NOT NULL,
  65. `user_groups` text NOT NULL,
  66. `user_level` tinyint(3) UNSIGNED NOT NULL DEFAULT '101',
  67. `user_status` tinyint(1) UNSIGNED NOT NULL DEFAULT '0',
  68. `user_prefix` char(3) NOT NULL DEFAULT '~',
  69. `user_color` varchar(6) NOT NULL DEFAULT '',
  70. `user_adds` tinyint(4) NOT NULL DEFAULT '0',
  71. `user_email_act` tinyint(1) NOT NULL DEFAULT '0',
  72. `user_rang` varchar(30) NOT NULL DEFAULT '',
  73. `user_points` smallint(5) DEFAULT '0',
  74. `points_normal` smallint(5) DEFAULT '0',
  75. `points_bonus` smallint(5) DEFAULT '0',
  76. `points_punishment` smallint(5) DEFAULT '0',
  77. PRIMARY KEY (`user_id`)
  78. ) TYPE=MyISAM AUTO_INCREMENT=2 ;
  79.  
  80. --
  81. -- Zrzut danych tabeli `ef_users`
  82. --
  83.  
  84. INSERT INTO `ef_users` VALUES (1, 'admin', 'xxx', 'xxx@wp.pl', 1, '', '0000-00-00', '', '', '', '', '', 'Default', '0', '', '', 0, 1217656567, 1219513455, '127.0.0.1', 'A.AC.AD.B.BP.C.CA.CO.CP.DB.DC.D.EP.FQ.FR.F.FU.IM.I.IP.M.N.NC.P.PH.PI.PO.RG.RS.S.
  85. L.S1.S2.S3.S4.S5.S6.S7.SU.UG.U.W.WC', '', 103, 0, '@', 'F75013', 0, 1, '', 0, 0, 0, 0);
wookieb
  1. SELECT a.group_cat, b.*, c.* FROM ef_users AS c LEFT JOIN ef_groups AS a ON (c.user_id=a.group_admin) INNER JOIN ef_groups_cats AS b ON (b.cat_id=a.group_cat)

I mi działa.
reyne
i mi też winksmiley.jpg dzięki za pomoc, pozdrawiam! cool.gif
To jest wersja lo-fi głównej zawartości. Aby zobaczyć pełną wersję z większą zawartością, obrazkami i formatowaniem proszę kliknij tutaj.
Invision Power Board © 2001-2025 Invision Power Services, Inc.