Solved

MySQL - need to remove duplicate records for query joining 3 tables.

Posted on 2012-12-28
2
266 Views
Last Modified: 2013-01-03
Hi,
I have 3 tables , an `items` table, a `category` table that an item belongs to and an `items_to_category` table which lists what category an item belongs to.
Note: an item may belong to more than one category.

I am having problems with duplicate items showing up for a query when I perform a keyword search.

When I search for keywords with below query I get duplicate items, what is wrong with query below?

SELECT DISTINCT items_to_category.item AS id, items.itemName, items.itemDesc, items.itemDesc2, items.itemDesc3, items.itemPrice, items.image1, items.image2, items.image3, items.image4, items.image5, items.cookie, items.item_type, items.min_order, items.prodcode, items.true_stock_count, items.display_stock_count, items.low_stock_count, items.limit_availability, items.date_time_expires, items.itemDescb, items.itemPrice, items.retPrice, items.specialPrice, items.specs1, items.spechead1, items.specs2, items.spechead2, items.add_comments, items.more_details, category, items_to_category.position, category.subcat, category.position as cat_order, category.cat_type FROM items_to_category LEFT JOIN category ON category.id = items_to_category.category LEFT JOIN items ON items.id = items_to_category.item WHERE (items.itemName LIKE '%cat%' || items.itemDesc LIKE '%cat%') AND items.itemName IS NOT NULL ORDER BY cat_order, category, position 

Open in new window


Can you please help.

Also queries below appear to work OK, but is there a better option?

Thanks in advance for your feedback

Single category.id search works Ok with below query
SELECT DISTINCT items_to_category.item AS id, items.itemName, items.itemDesc, items.itemDesc2, items.itemDesc3, items.itemPrice, items.image1, items.image2, items.image3, items.image4, items.image5, items.cookie, items.item_type, items.min_order, items.prodcode, items.true_stock_count, items.display_stock_count, items.low_stock_count, items.limit_availability, items.date_time_expires, items.itemDescb, items.itemPrice, items.retPrice, items.specialPrice, items.specs1, items.spechead1, items.specs2, items.spechead2, items.add_comments, items.more_details, category, items_to_category.position, category.subcat, category.position as cat_order, category.cat_type FROM items_to_category LEFT JOIN category ON category.id = items_to_category.category LEFT JOIN items ON items.id = items_to_category.item WHERE category.id IN ('118') ORDER BY cat_order, category, position 

Mutiple category.id's also work ok
SELECT DISTINCT items_to_category.item AS id, items.itemName, items.itemDesc, items.itemDesc2, items.itemDesc3, items.itemPrice, items.image1, items.image2, items.image3, items.image4, items.image5, items.cookie, items.item_type, items.min_order, items.prodcode, items.true_stock_count, items.display_stock_count, items.low_stock_count, items.limit_availability, items.date_time_expires, items.itemDescb, items.itemPrice, items.retPrice, items.specialPrice, items.specs1, items.spechead1, items.specs2, items.spechead2, items.add_comments, items.more_details, category, items_to_category.position, category.subcat, category.position as cat_order, category.cat_type FROM items_to_category LEFT JOIN category ON category.id = items_to_category.category LEFT JOIN items ON items.id = items_to_category.item WHERE category.id IN ('122','120','119') ORDER BY cat_order, category, position 

category.cat_type search works fine also
SELECT DISTINCT items_to_category.item AS id, items.itemName, items.itemDesc, items.itemDesc2, items.itemDesc3, items.itemPrice, items.image1, items.image2, items.image3, items.image4, items.image5, items.cookie, items.item_type, items.min_order, items.prodcode, items.true_stock_count, items.display_stock_count, items.low_stock_count, items.limit_availability, items.date_time_expires, items.itemDescb, items.itemPrice, items.retPrice, items.specialPrice, items.specs1, items.spechead1, items.specs2, items.spechead2, items.add_comments, items.more_details, category, items_to_category.position, category.subcat, category.position as cat_order, category.cat_type FROM items_to_category LEFT JOIN category ON category.id = items_to_category.category LEFT JOIN items ON items.id = items_to_category.item WHERE cat_type = 'GN' AND items.itemName IS NOT NULL ORDER BY cat_order, category, position 




--
-- Table structure for table `category`
--

CREATE TABLE IF NOT EXISTS `category` (
  `id` int(11) NOT NULL auto_increment,
  `locked` int(5) default '0',
  `position` int(5) default '10',
  `cat` varchar(40) default 'Main',
  `subcat` varchar(50) NOT NULL,
  `min_order` tinyint(3) default NULL,
  `cat_type` varchar(4) default 'GN',
  `active` char(3) default 'Y',
  UNIQUE KEY `id_2` (`id`),
  KEY `id` (`id`)
) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=153 ;

--
-- Dumping data for table `category`
--

INSERT INTO `category` (`id`, `locked`, `position`, `cat`, `subcat`, `min_order`, `cat_type`, `active`) VALUES
(118, NULL, 1, 'Main', 'Dog Products', NULL, 'SC', 'Y'),
(119, NULL, 2, 'Main', 'Cat Products', NULL, 'SC', 'Y'),
(120, NULL, 3, 'Main', 'Bird Products', NULL, 'SC', 'Y'),
(123, NULL, 21, 'Main', 'Frontline Plus', NULL, 'SM', 'Y'),
(122, NULL, 20, 'Main', 'Advance', NULL, 'SM', 'Y'),
(124, NULL, 22, 'Main', 'PURINA', NULL, 'SM', 'Y'),
(121, NULL, 4, 'Main', 'Fish Products', NULL, 'SC', 'Y'),
(125, NULL, 23, 'Main', 'Science Diet', NULL, 'SM', 'Y'),
(126, NULL, 24, 'Main', 'IAMS', NULL, 'SM', 'Y'),
(127, NULL, 10, 'Main', 'Home Page', NULL, 'SC', 'Y'),
(133, 1, 1, 'Main', 'Fade Show Image', NULL, 'GS', 'Y'),
(134, NULL, 10, 'Main', 'OUR CMS', NULL, 'BC', 'Y'),
(135, NULL, 10, 'Main', 'Test Blog', NULL, 'BC', 'Y'),
(136, NULL, 10, 'Main', 'Web Design', NULL, 'DL', 'Y'),
(137, NULL, 10, 'Main', 'Website Hosting', NULL, 'DL', 'Y'),
(138, NULL, 10, 'Main', 'Domain Names', NULL, 'DL', 'Y'),
(145, 1, 3, 'Main', 'Popular Products', NULL, 'SS', 'Y'),
(140, NULL, 10, 'Main', 'Membership', NULL, 'SC', 'Y'),
(147, 0, 10, 'Main', 'Recent Pictures', NULL, 'GN', 'Y'),
(146, 0, 10, 'Main', 'New Products', NULL, 'SC', 'Y'),
(148, 0, 10, 'Main', 'New Slider', NULL, 'GS', 'Y'),
(149, 0, 10, 'Main', 'Business Services', NULL, 'DL', 'Y'),
(150, 0, 10, 'Main', 'Limited Offers', NULL, 'SC', 'Y'),
(151, 0, 10, 'Main', 'Special Offers', NULL, 'SC', 'Y');

-- --------------------------------------------------------

--
-- Table structure for table `items`
--

CREATE TABLE IF NOT EXISTS `items` (
  `id` int(11) NOT NULL auto_increment,
  `item_type` enum('G','S','F') default 'F',
  `min_order` tinyint(3) default '1',
  `prodcode` varchar(10) default NULL,
  `true_stock_count` varchar(200) default '0',
  `display_stock_count` varchar(200) default '0',
  `low_stock_count` varchar(40) default '0',
  `date_time_expires` varchar(120) default NULL,
  `limit_availability` varchar(20) default NULL,
  `itemName` varchar(50) default NULL,
  `itemDesc` text,
  `itemDesc2` text,
  `itemDesc3` text,
  `weight` decimal(12,2) NOT NULL default '0.00',
  `height` mediumint(8) NOT NULL,
  `length` mediumint(8) NOT NULL,
  `width` mediumint(8) NOT NULL,
  `combined_postage` int(3) default '1',
  `item_postage` varchar(120) default NULL,
  `item_postage_amt` double(4,2) default NULL,
  `aust_post_estimate` varchar(240) default 'Regular Parcel - $6.60    Express Post - $9.55',
  `tabs_needed` tinyint(3) default '0',
  `tabhead1` varchar(60) default 'Description',
  `tabhead2` varchar(60) default NULL,
  `tabhead3` varchar(60) default NULL,
  `itemDescb` varchar(40) default NULL,
  `itemPrice` decimal(8,2) default '0.00',
  `retPrice` decimal(8,2) default '0.00',
  `specialPrice` decimal(8,2) default '0.00',
  `specs1` varchar(180) default NULL,
  `spechead1` varchar(180) default NULL,
  `specs2` varchar(180) default NULL,
  `spechead2` varchar(180) NOT NULL,
  `add_comments` char(1) default 'N',
  `more_details` enum('Y','N') default 'N',
  `image1` varchar(60) default '/uploads/nopic.jpg',
  `image2` varchar(60) default '/uploads/nopic.jpg',
  `image3` varchar(60) default '/uploads/nopic.jpg',
  `image4` varchar(60) default '/uploads/nopic.jpg',
  `image5` varchar(60) default '/uploads/nopic.jpg',
  `cookie` varchar(50) default NULL,
  PRIMARY KEY  (`id`),
  UNIQUE KEY `id` (`id`)
) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=1062 ;

--
-- Dumping data for table `items`
--

INSERT INTO `items` (`id`, `item_type`, `min_order`, `prodcode`, `true_stock_count`, `display_stock_count`, `low_stock_count`, `date_time_expires`, `limit_availability`, `itemName`, `itemDesc`, `itemDesc2`, `itemDesc3`, `weight`, `height`, `length`, `width`, `combined_postage`, `item_postage`, `item_postage_amt`, `aust_post_estimate`, `tabs_needed`, `tabhead1`, `tabhead2`, `tabhead3`, `itemDescb`, `itemPrice`, `retPrice`, `specialPrice`, `specs1`, `spechead1`, `specs2`, `spechead2`, `add_comments`, `more_details`, `image1`, `image2`, `image3`, `image4`, `image5`, `cookie`) VALUES
(782, NULL, NULL, NULL, '0', '0', '0', NULL, NULL, 'IAMS Smart Puppy Large Breed', '<p>\r\n	<span style="color: rgb(51, 51, 51); line-height: 20px; font-family: Arial, Helvetica-Normal, serif; font-size: 14px; font-weight: bold;">IAMS Smart Puppy Large Breed</span><br />\r\n	<br />\r\n	<strong><span style="color: rgb(51, 51, 51); line-height: 16px; font-family: Arial, Helvetica-Normal, serif; font-size: 12px;">Weight:3.6KG<br />\r\n	9KG +$32<br />\r\n	18KG +$74</span></strong><br />\r\n	<span style="color: rgb(51, 51, 51); line-height: 16px; font-family: Arial, Helvetica-Normal, serif; font-size: 12px;">&nbsp;<br />\r\n	This Iams ProActive Health formula has PreBiotics that work inside the digestive tract to promote healthy digestion and strong defenses. Healthy inside. Healthy outside.</span></p>\r\n<p>\r\n	<span style="color: rgb(51, 51, 51); line-height: 16px; font-family: Arial, Helvetica-Normal, serif; font-size: 12px;">Designed for dogs ages 1-24 months,Iams concentrated nutrition proactively nourishes your large-breed puppy to help keep him at his best.</span></p>\r\n<p>\r\n	<span style="color: rgb(51, 51, 51); line-height: 16px; font-family: Arial, Helvetica-Normal, serif; font-size: 12px;">Natural fish oils with DHA (Docosahexaenoic Acid), a nutrient vital for optimal brain development, promote smarter, more trainable puppies. Protein sourced from chicken and egg helps build strong, firm muscles. Seven essential nutrients nourish the heart while essential minerals promote strong teeth and bones, and antioxidants help maintain a strong immune system.</span></p>\r\n<p>\r\n	<span style="color: rgb(51, 51, 51); line-height: 16px; font-family: Arial, Helvetica-Normal, serif; font-size: 12px;">A high-quality protein with no fillers or artificial preservatives, Iams is gentle on the digestive system. Fuel your puppy’s healthy development with 100% complete and balanced nutrition that’s vet recommended and 100% Satisfaction Guaranteed.</span></p>\r\n', NULL, NULL, 4.00, 15, 83, 41, 1, 'flat_rate', 20.00, ' Regular Parcel - $23.30    Express Post - $70.10', 1, 'Description', NULL, NULL, NULL, 25.95, NULL, 0.00, NULL, NULL, NULL, '', 'N', NULL, '/uploads/item782_1.jpg', '/uploads/nopic.jpg', '/uploads/nopic.jpg', '/uploads/nopic.jpg', 'uploads/nopic.jpg', NULL),
(762, NULL, NULL, NULL, '48,24,22,8,12,9', '48,24,22,8,12,9', '0', '29/12/2012 04:25 ', 'TO', 'Font Line Plus for Cats', '<p>\r\n	<span style="line-height: 20px; font-family: Arial, Helvetica-Normal, serif; color: rgb(51,51,51); font-size: 14px; font-weight: bold"><span style="margin-top: 55px">Font Line Plus for Cats</span></span></p>\r\n', NULL, NULL, 16.00, 21, 47, 19, 3, 'aust_post', 0.00, '&nbsp;Regular Parcel - $26.15&nbsp;&nbsp;&nbsp;&nbsp;Express Post - $83.30', 1, 'Description', NULL, NULL, NULL, 44.00, NULL, 33.00, '5kg,6kg,8kg', 'Weight', 'red,blue,green', 'Colour', 'N', NULL, '/uploads/item762_1.jpg', '/uploads/nopic.jpg', '/uploads/nopic.jpg', '/uploads/nopic.jpg', 'uploads/nopic.jpg', NULL),
(768, NULL, NULL, NULL, '0', '0', '0', NULL, NULL, 'Advance Adult Dog Small Breed', '<p>\r\n	<span style="color: rgb(51, 51, 51); line-height: 20px; font-family: Arial, Helvetica-Normal, serif; font-size: 14px; font-weight: bold;">Advance Adult Dog Small Breed</span></p>\r\n<p>\r\n	<strong><span style="color: rgb(51, 51, 51); line-height: 16px; font-family: Arial, Helvetica-Normal, serif; font-size: 12px;">Weight: 1.5KG<br />\r\n	2.5KG +$8.85<br />\r\n	7KG +$41.15<br />\r\n	13KG +$77.80</span></strong><br />\r\n	<span style="color: rgb(51, 51, 51); line-height: 16px; font-family: Arial, Helvetica-Normal, serif; font-size: 12px;">&nbsp;<br />\r\n	Adult maintenance food ideal for dogs of normal activity level<br />\r\n	Turkey and Rice as main source of high quality protein<br />\r\n	Beneficial in managing dietary sensitivity<br />\r\n	Formulated to help maintain urinary tract health<br />\r\n	Smaller kibble size ideal for toy to medium sized dogs<br />\r\n	High palatability to tempt fussier eaters </span></p>\r\n', NULL, NULL, 1.50, 5, 16, 16, 1, 'aust_post', 34.55, '&nbsp;Regular Parcel - $12.85&nbsp;&nbsp;&nbsp;&nbsp;Express Post - $21.70', 1, 'Description', NULL, NULL, NULL, 16.40, NULL, 0.00, NULL, NULL, NULL, '', 'N', NULL, '/uploads/item768_1.jpg', '/uploads/nopic.jpg', '/uploads/nopic.jpg', '/uploads/nopic.jpg', 'uploads/nopic.jpg', NULL),
(772, NULL, NULL, NULL, '0', '0', '0', '30/06/2012 00:00 ', 'TO', 'Advance Dog Dental Large and Giant Breeds', '<p>\r\n	<span style="color: rgb(51, 51, 51); line-height: 20px; font-family: Arial, Helvetica-Normal, serif; font-size: 14px; font-weight: bold;">Advance Dog Dental Large and Giant Breeds</span><br />\r\n	<br />\r\n	<strong><span style="color: rgb(51, 51, 51); line-height: 16px; font-family: Arial, Helvetica-Normal, serif; font-size: 12px;">Weight: 3KG<br />\r\n	8KG +$39.20<br />\r\n	15KG +$84.20</span></strong><br />\r\n	<span style="color: rgb(51, 51, 51); line-height: 16px; font-family: Arial, Helvetica-Normal, serif; font-size: 12px;">&nbsp;<br />\r\n	For Large and Giant sized Breeds, has specifically designed larger kibble and active ingredients which help to promote and maintain dental health. </span></p>\r\n', NULL, NULL, 0.50, 5, 3, 5, 1, 'free_postage', 0.00, 'Regular Parcel - $6.60    Express Post - $9.55', 1, 'Description', NULL, NULL, NULL, 35.05, NULL, 24.00, NULL, NULL, NULL, '', 'N', NULL, '/uploads/item772_1.jpg', '/uploads/nopic.jpg', '/uploads/nopic.jpg', '/uploads/nopic.jpg', 'uploads/nopic.jpg', NULL),
(786, NULL, NULL, NULL, '0', '0', '0', NULL, NULL, 'Science Diet® - Puppy Small Bites', '<p>\r\n	<span style="color: rgb(51, 51, 51); line-height: 20px; font-family: Arial, Helvetica-Normal, serif; font-size: 14px; font-weight: bold;">Science Diet® - Puppy Small Bites</span><br />\r\n	<br />\r\n	<strong><span style="color: rgb(51, 51, 51); line-height: 16px; font-family: Arial, Helvetica-Normal, serif; font-size: 12px;">Rich in fish oil and nourishing fatty acids.</span></strong></p>\r\n<p>\r\n	<span style="color: rgb(51, 51, 51); line-height: 16px; font-family: Arial, Helvetica-Normal, serif; font-size: 12px;">Hill''s® Science Diet® Puppy Small Bites dog food provides precisely balanced nutrition to build immunity and enhance mobility for puppies who prefer a smaller kibble. It is enriched with DHA from high quality fish oil and made with high quality ingredients that provide an ideal combination of nutrients, clinically proven antioxidants and vital fatty acids to give your puppy the best start in life.</span></p>\r\n<p>\r\n	<br />\r\n	&nbsp;</p>\r\n', NULL, NULL, 5.00, 17, 12, 17, 3, 'aust_post', 0.00, '&nbsp;Regular Parcel - $15.70&nbsp;&nbsp;&nbsp;&nbsp;Express Post - $34.90', 1, 'Description', NULL, NULL, NULL, 25.00, NULL, 0.00, NULL, NULL, NULL, '', 'N', NULL, '/uploads/item786_1.jpg', '/uploads/item786_2.jpg', '/uploads/nopic.jpg', '/uploads/nopic.jpg', 'uploads/nopic.jpg', NULL),
(1019, NULL, NULL, NULL, '0', '0', '0', NULL, NULL, 'IAMS ', '<p>\r\n	<span style="color: rgb(51, 51, 51); line-height: 20px; font-family: verdana,arial,helvetica-normal,serif; font-size: 18px; font-weight: normal;">Cat Food</span></p>\r\n', NULL, NULL, 0.50, 29, 10, 8, 1, 'flat_rate', 66.30, ' Regular Parcel - $6.60    Express Post - $9.55', NULL, 'Description', NULL, NULL, NULL, 56.88, NULL, 46.00, NULL, NULL, NULL, '', 'N', NULL, '/uploads/item1019_1.jpg', '/uploads/nopic.jpg', '/uploads/nopic.jpg', '/uploads/nopic.jpg', '/uploads/nopic.jpg', NULL),
(936, 'F', NULL, NULL, '0', '0', '0', NULL, NULL, NULL, NULL, NULL, NULL, 0.00, 0, 0, 0, 1, NULL, NULL, 'Regular Parcel - $6.60    Express Post - $9.55', NULL, 'Description', NULL, NULL, NULL, 0.00, 0.00, 0.00, NULL, NULL, NULL, '', 'N', NULL, '/uploads/item936_1.jpg', '/uploads/nopic.jpg', '/uploads/nopic.jpg', '/uploads/nopic.jpg', '/uploads/nopic.jpg', NULL),
(872, NULL, NULL, NULL, '0', '0', '0', NULL, NULL, 'Gold Listing', '<p>\r\n	<span style="color: rgb(50, 186, 242); line-height: 20px; font-family: verdana, arial, helvetica-normal, serif; font-size: 18px; font-weight: normal;">12 months Gold Listing</span></p>\r\n', NULL, NULL, 0.50, 5, 3, 5, 1, 'flat_rate', 17.00, 'Regular Parcel - $6.60    Express Post - $9.55', 1, 'Description', NULL, NULL, NULL, 299.00, NULL, 39.99, NULL, NULL, NULL, '', 'N', NULL, '/uploads/item872_1.jpg', '/uploads/nopic.jpg', '/uploads/nopic.jpg', '/uploads/nopic.jpg', '/uploads/nopic.jpg', NULL),
(874, NULL, NULL, NULL, '0', '0', '0', NULL, NULL, 'Silver Membership', '<p>\r\n	<span style="color: rgb(50, 186, 242); line-height: 20px; font-family: verdana, arial, helvetica-normal, serif; font-size: 18px; font-weight: normal;">Silver Membership</span></p>\r\n', NULL, NULL, 0.50, 5, 3, 5, 1, 'aust_post', 0.00, '&nbsp;Regular Parcel - $6.60&nbsp;&nbsp;&nbsp;&nbsp;Express Post - $9.55', 1, 'Description', NULL, NULL, NULL, 99.00, NULL, 0.00, NULL, NULL, NULL, '', 'N', NULL, '/uploads/item874_1.jpg', '/uploads/nopic.jpg', '/uploads/nopic.jpg', '/uploads/nopic.jpg', '/uploads/nopic.jpg', NULL),
(876, NULL, NULL, NULL, '0', '0', '0', NULL, NULL, 'Free Membership', '<p>\r\n	<span style="line-height: 20px; font-family: verdana, arial, helvetica-normal, serif; color: rgb(51,51,51); font-size: 18px; font-weight: normal">Free Membership - 12 months </span></p>\r\n<p>\r\n	<span style="line-height: 16px; font-family: verdana, arial, helvetica-normal, serif; color: rgb(51,51,51); font-size: 14px; font-weight: normal">Directory Listing - List your business with us for FREE</span></p>\r\n', NULL, NULL, 0.50, 5, 3, 5, 10, 'aust_post', 0.00, ' Regular Parcel - $6.60    Express Post - $9.55', 1, 'Description', NULL, NULL, NULL, 0.00, NULL, 0.00, NULL, NULL, NULL, '', 'N', NULL, '/uploads/item876_1.jpg', '/uploads/nopic.jpg', '/uploads/nopic.jpg', '/uploads/nopic.jpg', '/uploads/nopic.jpg', NULL),
(882, NULL, NULL, NULL, '0', '0', '0', NULL, NULL, 'Bird Food', '<p>\r\n	<span style="color: rgb(51, 51, 51); line-height: 20px; font-family: verdana, arial, helvetica-normal, serif; font-size: 18px; font-weight: normal;">Bird Food</span></p>\r\n', NULL, NULL, 0.50, 5, 3, 5, 1, 'flat_rate', 5.00, 'Regular Parcel - $6.60    Express Post - $9.55', 1, 'Description', NULL, NULL, NULL, 55.00, NULL, 45.00, NULL, NULL, NULL, '', 'N', NULL, '/uploads/item882_1.jpg', '/uploads/nopic.jpg', '/uploads/nopic.jpg', '/uploads/nopic.jpg', '/uploads/nopic.jpg', NULL),
(908, 'F', NULL, NULL, '0', '0', '0', NULL, NULL, 'Koi Balance', '<p>\r\n	<span style="color: rgb(51, 51, 51); line-height: 20px; font-family: verdana, arial, helvetica-normal, serif; font-size: 18px; font-weight: normal;">Koi Balance - fish Food</span></p>\r\n', NULL, NULL, 0.00, 0, 0, 0, 1, NULL, NULL, 'Regular Parcel - $6.60    Express Post - $9.55', 1, 'Description', NULL, NULL, NULL, 55.00, 0.00, 0.00, NULL, NULL, NULL, '', 'N', NULL, '/uploads/item908_1.jpg', '/uploads/nopic.jpg', '/uploads/nopic.jpg', '/uploads/nopic.jpg', '/uploads/nopic.jpg', NULL),
(886, 'F', NULL, NULL, '0', '0', '0', NULL, NULL, 'Gallery Image', '<p>\r\n	<span style="line-height: 20px; font-family: verdana, arial, helvetica-normal, serif; color: rgb(51,51,51); font-size: 18px; font-weight: normal">Test Gallery</span></p>\r\n', NULL, NULL, 0.00, 0, 0, 0, 1, NULL, NULL, 'Regular Parcel - $6.60    Express Post - $9.55', 1, 'Description', NULL, NULL, NULL, 55.00, 0.00, 23.00, NULL, NULL, NULL, '', 'N', NULL, '/uploads/item886_1.jpg', '/uploads/nopic.jpg', '/uploads/nopic.jpg', '/uploads/nopic.jpg', '/uploads/nopic.jpg', NULL),
(926, 'F', NULL, NULL, '0', '0', '0', NULL, NULL, NULL, NULL, NULL, NULL, 0.00, 0, 0, 0, 1, NULL, NULL, 'Regular Parcel - $6.60    Express Post - $9.55', NULL, 'Description', NULL, NULL, NULL, 0.00, 0.00, 0.00, NULL, NULL, NULL, '', 'N', NULL, '/uploads/item926_1.jpg', '/uploads/nopic.jpg', '/uploads/nopic.jpg', '/uploads/nopic.jpg', '/uploads/nopic.jpg', NULL),
(928, 'F', NULL, NULL, '0', '0', '0', NULL, NULL, NULL, NULL, NULL, NULL, 0.00, 0, 0, 0, 1, NULL, NULL, 'Regular Parcel - $6.60    Express Post - $9.55', NULL, 'Description', NULL, NULL, NULL, 0.00, 0.00, 0.00, NULL, NULL, NULL, '', 'N', NULL, '/uploads/item928_1.jpg', '/uploads/nopic.jpg', '/uploads/nopic.jpg', '/uploads/nopic.jpg', '/uploads/nopic.jpg', NULL),
(930, 'F', NULL, NULL, '0', '0', '0', NULL, NULL, 'Test', NULL, NULL, NULL, 0.00, 0, 0, 0, 1, NULL, NULL, 'Regular Parcel - $6.60    Express Post - $9.55', NULL, 'Description', NULL, NULL, NULL, 0.00, 0.00, 0.00, NULL, NULL, NULL, '', 'N', NULL, '/uploads/item930_1.jpg', '/uploads/nopic.jpg', '/uploads/nopic.jpg', '/uploads/nopic.jpg', '/uploads/nopic.jpg', NULL),
(932, 'F', NULL, NULL, '0', '0', '0', NULL, NULL, NULL, NULL, NULL, NULL, 0.00, 0, 0, 0, 1, NULL, NULL, 'Regular Parcel - $6.60    Express Post - $9.55', NULL, 'Description', NULL, NULL, NULL, 0.00, 0.00, 0.00, NULL, NULL, NULL, '', 'N', NULL, '/uploads/item932_1.jpg', '/uploads/nopic.jpg', '/uploads/nopic.jpg', '/uploads/nopic.jpg', '/uploads/nopic.jpg', NULL),
(934, 'F', NULL, NULL, '0', '0', '0', NULL, NULL, 'Test', NULL, NULL, NULL, 0.00, 0, 0, 0, 1, NULL, NULL, 'Regular Parcel - $6.60    Express Post - $9.55', NULL, 'Description', NULL, NULL, NULL, 0.00, 0.00, 0.00, NULL, NULL, NULL, '', 'N', NULL, '/uploads/item934_1.jpg', '/uploads/nopic.jpg', '/uploads/nopic.jpg', '/uploads/nopic.jpg', '/uploads/nopic.jpg', NULL),
(906, NULL, NULL, NULL, '4', '4', '0', '27/02/2013 00:00 ', 'TO', 'Bird Food', '<p>\r\n	<span style="color: rgb(51, 51, 51); line-height: 16px; font-family: verdana, arial, helvetica-normal, serif; font-size: 14px; font-weight: normal;">Bird Food</span></p>\r\n', NULL, NULL, 14.50, 18, 3, 17, 1, 'aust_post', 0.00, ' Regular Parcel - $25.20    Express Post - $78.90', 1, 'Description', NULL, NULL, NULL, 44.00, NULL, 0.00, NULL, NULL, NULL, '', 'N', NULL, '/uploads/item906_1.jpg', '/uploads/nopic.jpg', '/uploads/nopic.jpg', '/uploads/nopic.jpg', '/uploads/nopic.jpg', NULL),
(1045, NULL, NULL, NULL, '0', '0', '0', NULL, NULL, 'testing', NULL, NULL, NULL, 6.50, 30, 3, 5, 1, 'aust_post', 0.00, '&nbsp;Regular Parcel - $17.60&nbsp;&nbsp;&nbsp;&nbsp;Express Post - $43.70', NULL, 'Description', NULL, NULL, NULL, 44.00, NULL, 32.00, NULL, NULL, NULL, '', 'N', NULL, '/uploads/nopic.jpg', '/uploads/nopic.jpg', '/uploads/nopic.jpg', '/uploads/nopic.jpg', '/uploads/nopic.jpg', NULL);

-- --------------------------------------------------------

--
-- Table structure for table `items_to_category`
--

CREATE TABLE IF NOT EXISTS `items_to_category` (
  `category` int(11) NOT NULL,
  `item` int(11) NOT NULL,
  `position` int(11) default NULL,
  PRIMARY KEY  (`category`,`item`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1;

--
-- Dumping data for table `items_to_category`
--

INSERT INTO `items_to_category` (`category`, `item`, `position`) VALUES
(145, 782, 5),
(118, 782, 1),
(118, 786, 3),
(126, 762, 1),
(145, 1019, 2),
(145, 768, 1),
(122, 1045, 1),
(119, 762, 1),
(118, 768, 2),
(150, 772, 1),
(140, 872, 1),
(140, 874, 2),
(140, 876, 3),
(145, 882, 4),
(145, 772, 3),
(133, 788, 8),
(134, 145, NULL),
(133, 908, 4),
(147, 908, 3),
(133, 926, 3),
(147, 926, 4),
(133, 928, 7),
(147, 928, 5),
(150, 762, 2),
(120, 906, 1),
(133, 936, 6),
(133, 886, 5),
(133, 930, 2),
(147, 932, 2),
(147, 934, 1),
(133, 852, 1),
(123, 762, 1);

Open in new window

0
Comment
Question by:sabecs
2 Comments
 
LVL 13

Accepted Solution

by:
AielloJ earned 300 total points
ID: 38729201
sabecs:

When you use DISTINCT, all columns in the query must be distinct.  In your example query, several columns are different (id, category, position, subcat, and cat_order).  Removing them from the query (as shown below) eliminates the duplicates.

SELECT DISTINCT items.itemName, items.itemDesc, items.itemDesc2,
items.itemDesc3, items.itemPrice, items.image1, items.image2, items.image3, items.image4,
items.image5, items.cookie, items.item_type, items.min_order, items.prodcode, items.true_stock_count,
items.display_stock_count, items.low_stock_count, items.limit_availability, items.date_time_expires,
items.itemDescb, items.itemPrice, items.retPrice, items.specialPrice, items.specs1, items.spechead1,
items.specs2, items.spechead2, items.add_comments, items.more_details  
FROM items_to_category
LEFT JOIN category
ON category.id = items_to_category.category
LEFT JOIN items
ON items.id = items_to_category.item
WHERE (items.itemName LIKE '%cat%' || items.itemDesc LIKE '%cat%')
AND items.itemName IS NOT NULL

Best regards,

AielloJ
0
 
LVL 59

Assisted Solution

by:Kevin Cross
Kevin Cross earned 200 total points
ID: 38729221
If you need the information, in MySQL you can use GROUP_CONCAT(). You may want to move the focus from items_to_category to items. Because you use a filter on items in the WHERE, the JOIN to items is acting as an INNER JOIN anyway; therefore, you should be able to make it the main table of the query. Then LEFT OUTER JOIN the categories similar to the following.

FROM items i
LEFT OUTER JOIN (
    SELECT items_to_category.item
         , GROUP_CONCAT(category.cat) AS cats
    FROM items_to_category 
    JOIN category 
      ON category.id = items_to_category.category 
) c ON c.item = i.id

Open in new window

0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Introduction Since I wrote the original article about Handling Date and Time in PHP and MySQL (http://www.experts-exchange.com/articles/201/Handling-Date-and-Time-in-PHP-and-MySQL.html) several years ago, it seemed like now was a good time to updat…
Creating and Managing Databases with phpMyAdmin in cPanel.
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

746 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now