Witam

W bazie są następujące tabele
  • categories (category_id level sort) category_id - numer kategorii, level - kategoria nadrzędna(rodzic), sort - kolejność wyświetlania
  • cat_translations ( cid cat_name ) cid - numer kategorii, cat_name - nazwa kategorii
  • products (product_id category_id category2 category3) product_id - id produktu, category_id - id kategorii, category2 -druga kategoria category3 - trzecia kategoria
  • prod_translations (pid product_name ... i inne pola opisujące) pid - id produktu
Jak sformułować zapytanie aby otrzymać listę produktów w określonej kategorii ze zmiennej GET np. $_GET['kat'] = 5
... i jeśli jest to kategoria główna-rodzic to razem z produktami w jej podkategoriach ?

Próbuję tak, ale efekty beznadziejne:
  1. <?php
  2. $rekord_kategoria = mysql_query(&#092;" SELECT  *
  3. FROM `products`  
  4. LEFT JOIN `categories` ON (categories.level = products.category_id  OR categories.level = products.category2 OR categories.level = products.category3)
  5. LEFT JOIN  `prod_translations` ON  (`prod_translations`.`pid` = `products`.`product_id` )
  6. LEFT JOIN  `gfx` ON  (prod_translations.pid = gfx.prod_id )
  7. WHERE  
  8. categories.level = '$kat' AND
  9. prod_translations.lang = 'pl'  AND
  10. prod_translations.active = 1
  11. ORDER BY products.add_date DESC  LIMIT &#092;".($pagz*$ilez).\", $ilez\") or die(mysql_error());
  12. ?>



  1. --
  2. -- Struktura tabeli dla `categories`
  3. --
  4.  
  5. CREATE TABLE `categories` (
  6. `category_id` int(11) NOT NULL AUTO_INCREMENT,
  7. `level` varchar(255) NOT NULL,
  8. `sort` smallint(4) NOT NULL DEFAULT '0',
  9. PRIMARY KEY (`category_id`),
  10. KEY `level_2` (`level`),
  11. KEY `sort` (`sort`),
  12. FULLTEXT KEY `level` (`level`)
  13. ) ENGINE=MyISAM DEFAULT CHARSET=utf8 PACK_KEYS=0 AUTO_INCREMENT=115 ;
  14.  
  15.  
  16. --
  17. -- Struktura tabeli dla `cat_translations`
  18. --
  19.  
  20. CREATE TABLE `cat_translations` (
  21. `trans_cid` int(11) UNSIGNED NOT NULL AUTO_INCREMENT,
  22. `cid` int(11) NOT NULL DEFAULT '0',
  23. `cat_name` varchar(99) NOT NULL,
  24. `title` varchar(99) NOT NULL DEFAULT '',
  25. `description` mediumtext NOT NULL,
  26. `active` tinyint(1) NOT NULL,
  27. `pres_id` int(11) UNSIGNED DEFAULT NULL,
  28. `lang` char(3) NOT NULL DEFAULT '',
  29. `products` int(10) UNSIGNED NOT NULL DEFAULT '0',
  30. `isdefault` tinyint(1) NOT NULL,
  31. PRIMARY KEY (`trans_cid`),
  32. KEY `cid` (`cid`),
  33. KEY `pres_id` (`pres_id`),
  34. KEY `cat_name` (`cat_name`,`lang`),
  35. KEY `active` (`active`,`lang`),
  36. KEY `lang` (`lang`),
  37. KEY `isdefault` (`isdefault`)
  38. ) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=119 ;
  39.  
  40.  
  41.  
  42. --
  43. -- Struktura tabeli dla `products`
  44. --
  45.  
  46. CREATE TABLE `products` (
  47. `product_id` int(11) UNSIGNED NOT NULL AUTO_INCREMENT,
  48. `category_id` int(11) UNSIGNED NOT NULL DEFAULT '0',
  49. `producer_id` int(11) UNSIGNED DEFAULT NULL,
  50. `rate` float DEFAULT NULL,
  51. `votes` int(6) DEFAULT NULL,
  52. `vat` varchar(8) NOT NULL DEFAULT '0',
  53. `in_stock` int(11) NOT NULL DEFAULT '1',
  54. `gfx` varchar(99) DEFAULT NULL,
  55. `gfx_small` varchar(30) DEFAULT NULL,
  56. `gfx_enlarge` varchar(30) DEFAULT NULL,
  57. `file` varchar(255) DEFAULT NULL,
  58. `add_date` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
  59. `edit_date` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  60. `price` decimal(10,2) NOT NULL DEFAULT '0.00',
  61. `promo` char(1) NOT NULL DEFAULT '',
  62. `warranty` int(11) NOT NULL DEFAULT '0',
  63. `weight` varchar(12) NOT NULL DEFAULT '',
  64. `views` int(7) NOT NULL DEFAULT '0',
  65. `category2` int(11) UNSIGNED NOT NULL DEFAULT '0',
  66. `category3` int(11) UNSIGNED NOT NULL DEFAULT '0',
  67. `sort` int(11) NOT NULL DEFAULT '0',
  68. `main_page` tinyint(1) NOT NULL,
  69. `main_page_sort` int(11) NOT NULL DEFAULT '0',
  70. `products_related` mediumtext NOT NULL,
  71. `other_price` float NOT NULL DEFAULT '0',
  72. `product_code` varchar(255) NOT NULL DEFAULT '',
  73. `pkwiu` varchar(255) DEFAULT NULL,
  74. PRIMARY KEY (`product_id`),
  75. UNIQUE KEY `product_code_2` (`product_code`),
  76. KEY `producer_id` (`producer_id`),
  77. KEY `add_date` (`add_date`),
  78. KEY `sort` (`sort`),
  79. KEY `categories` (`category_id`,`category2`,`category3`),
  80. KEY `main_page` (`main_page`),
  81. KEY `main_page_sort` (`main_page_sort`),
  82. KEY `price` (`price`),
  83. KEY `in_stock` (`in_stock`),
  84. KEY `promo` (`promo`),
  85. KEY `category2` (`category2`),
  86. KEY `category3` (`category3`)
  87. ) ENGINE=InnoDB DEFAULT CHARSET=utf8 PACK_KEYS=0 AUTO_INCREMENT=2021 ;
  88.  
  89.  
  90.  
  91. --
  92. -- Struktura tabeli dla `prod_translations`
  93. --
  94.  
  95. CREATE TABLE `prod_translations` (
  96. `id_transl` int(11) UNSIGNED NOT NULL AUTO_INCREMENT,
  97. `pid` int(11) NOT NULL DEFAULT '0',
  98. `product_name` varchar(255) character SET utf8 collate utf8_polish_ci NOT NULL,
  99. `description` mediumtext character SET utf8 collate utf8_polish_ci NOT NULL,
  100. `params` mediumtext NOT NULL,
  101. `jm` varchar(32) NOT NULL DEFAULT '',
  102. `options` mediumtext NOT NULL,
  103. `active` tinyint(1) NOT NULL,
  104. `lang` char(3) NOT NULL DEFAULT '',
  105. `isdefault` tinyint(1) NOT NULL,
  106. PRIMARY KEY (`id_transl`),
  107. UNIQUE KEY `pid` (`pid`,`lang`),
  108. KEY `lang_active` (`active`,`lang`),
  109. KEY `product_name_2` (`product_name`),
  110. KEY `pid_2` (`pid`),
  111. KEY `product_name_3` (`product_name`,`lang`),
  112. KEY `lang` (`lang`),
  113. KEY `isdefault` (`isdefault`),
  114. FULLTEXT KEY `product_name` (`product_name`),
  115. FULLTEXT KEY `search` (`product_name`,`description`)
  116. ) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=3904 ;



Proszę o pomoc.

Zrobiłem tak, ale jesli produkt jest dodany do więcej niż jednej kategorii to wyswietla się wielokrotnie...
  1. <?php
  2. $rekord_kategoria = mysql_query(&#092;" SELECT  *
  3. FROM `cat_translations`
  4. LEFT JOIN  `categories` ON cat_translations.cid  = categories.category_id
  5. LEFT JOIN `products` ON  (`products`.`category_id` = `cat_translations`.`cid`  OR `products`.`category2` = `cat_translations`.`cid` OR `products`.`category3` = `cat_translations`.`cid`)
  6. LEFT JOIN `prod_translations` ON products.product_id = prod_translations.pid
  7. JOIN  `gfx` ON  products.product_id = gfx.prod_id  
  8.  
  9. WHERE  
  10. categories.level = '$kat' OR categories.category_id = '$kat'
  11. AND gfx.gfx_sort = 1  AND gfx.main_gfx = 1
  12. AND prod_translations.lang = 'pl'  
  13. AND cat_translations.lang = 'pl'  
  14.  
  15. ORDER BY cat_translations.cid DESC  LIMIT &#092;".($pagz*$ilez).\", $ilez\") or die(mysql_error());
  16. ?>


Proszę o jakieś wskazówki....