troubleshooting Question

MySQL - combine 2 tables

Avatar of sabecs
sabecs asked on
MySQL Server
5 Comments1 Solution435 ViewsLast Modified:
Hi,
Can anyone please tell me how to combine the 2 tables below where cart.items_id = items.id
I only need the itemPrice from the second table displayed as well as all fields from first table and group by `cookieId` and order by `timestamp`?  

Thanks in advance for your feedback.

CREATE TABLE IF NOT EXISTS `cart` (
  `cartId` int(11) NOT NULL AUTO_INCREMENT,
  `cookieId` varchar(50) DEFAULT NULL,
  `items_id` int(11) DEFAULT NULL,
  `spec1` varchar(40) DEFAULT NULL,
  `spec2` varchar(40) DEFAULT NULL,
  `qty` int(11) DEFAULT NULL,
  `comments` tinytext,
  `timestamp` timestamp NULL DEFAULT CURRENT_TIMESTAMP,
  UNIQUE KEY `id` (`cartId`)
) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=70728 ;

--
-- Dumping data for table `cart`
--

INSERT INTO `cart` (`cartId`, `cookieId`, `items_id`, `spec1`, `spec2`, `qty`, `comments`, `timestamp`) VALUES
(1, '1', 249, 'Flakes', NULL, 44, NULL, '2009-06-06 04:27:44'),
(5, '4', 266, '', NULL, 14, NULL, '2009-06-06 05:08:08'),
(4, '2', 249, 'Flakes', NULL, 4, NULL, '2009-06-06 05:01:16'),
(6, '3', 278, '', NULL, 134, NULL, '2009-06-07 00:21:54'),
(8, 'dd19856f2521a5c2d1737cc2264b8762', 266, '', NULL, 12, NULL, '2009-06-07 01:08:34'),
(9, '60e108e5941bf500abfc8a38cfbe8a30', 255, 'Sprinkles', NULL, 1, NULL, '2009-06-07 01:09:36'),
(10, 'dd19856f2521a5c2d1737cc2264b8762', 263, '', NULL, 1, NULL, '2009-06-07 01:09:43'),
(11, '5', 280, '', NULL, 1, NULL, '2009-06-08 21:08:47'),
(12, '5', 282, '', NULL, 1, NULL, '2009-06-08 21:08:53'),
(13, '5', 289, '', NULL, 12, NULL, '2009-06-09 00:15:59');



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(240) 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(250) DEFAULT '/uploads/nopic.jpg',
  `image2` varchar(250) DEFAULT '/uploads/nopic.jpg',
  `image3` varchar(250) DEFAULT '/uploads/nopic.jpg',
  `image4` varchar(250) DEFAULT '/uploads/nopic.jpg',
  `image5` varchar(250) DEFAULT '/uploads/nopic.jpg',
  `cookie` varchar(50) DEFAULT NULL,
  `sortid` tinyint(5) NOT NULL,
  `points` tinyint(3) NOT NULL,
  PRIMARY KEY (`id`),
  UNIQUE KEY `id` (`id`)
) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=1920 ;

--
-- 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`, `sortid`, `points`) VALUES
(266, NULL, NULL, 'Passionfru', NULL, NULL, NULL, '', 'NL', 'Passionfruit delight', '<p>\r\n	Vanilla cupcake with passionfruit icing and a classic blossom.</p>\r\n', NULL, NULL, 0.00, 0, 0, 0, 1, NULL, NULL, 'Regular Parcel - $6.60    Express Post - $9.55', NULL, NULL, NULL, NULL, NULL, 3.50, NULL, NULL, NULL, NULL, NULL, '', 'N', NULL, 'uploads/PN266_1.jpg', 'uploads/nopic.jpg', 'uploads/nopic.jpg', 'uploads/nopic.jpg', '/uploads/nopic.jpg', NULL, 1, 3),
(270, 'S', NULL, 'Daisy', '0', '-3', NULL, '', 'NL', 'Decadent daisy ', 'Chocolate or vanilla cupcake with a decadent daisy on top. ', NULL, NULL, 0.00, 0, 0, 0, 1, NULL, NULL, 'Regular Parcel - $6.60    Express Post - $9.55', 0, 'Description', NULL, NULL, NULL, 3.50, NULL, NULL, 'Chocolate, vanilla', 'Flavour', NULL, '', 'N', 'N', 'uploads/PN270_1.jpg', 'uploads/nopic.jpg', 'uploads/nopic.jpg', 'uploads/nopic.jpg', '/uploads/nopic.jpg', NULL, 0, 3),
(272, 'S', NULL, 'Caramel', '0', '0', NULL, '', 'NL', 'Caramel delight', 'Vanilla or chocolate base cupcake with caramel swirl.', NULL, NULL, 0.00, 0, 0, 0, 1, NULL, NULL, 'Regular Parcel - $6.60    Express Post - $9.55', 0, 'Description', NULL, NULL, NULL, 3.50, NULL, NULL, 'Chocolate, vanilla', 'Flavour', NULL, '', 'N', 'N', 'uploads/PN272_1.jpg', 'uploads/nopic.jpg', 'uploads/nopic.jpg', 'uploads/nopic.jpg', '/uploads/nopic.jpg', NULL, 10, 3),
(274, NULL, NULL, 'Belle flow', NULL, NULL, NULL, NULL, 'NL', 'Belle flower cupcake', '<p>\r\n	Vanilla cupcake with strawberry icing, silver balls and three belle flowers.</p>\r\n', NULL, NULL, 0.00, 0, 0, 0, NULL, NULL, NULL, NULL, NULL, 'Description', NULL, NULL, NULL, 3.50, NULL, NULL, 'Chocolate, vanilla', 'Flavour', NULL, '', 'N', NULL, '/uploads/belle_flower_cupcake_274_1.jpg', 'uploads/nopic.jpg', 'uploads/nopic.jpg', 'uploads/nopic.jpg', '/uploads/nopic.jpg', NULL, 0, 3),
(416, 'S', NULL, 'Lemon coco', '0', '0', NULL, '', 'NL', 'Lemon & coconut', 'Our vanilla cupcake with lemon and coconut icing and a small rose flower', NULL, NULL, 0.00, 0, 0, 0, 1, NULL, NULL, 'Regular Parcel - $6.60    Express Post - $9.55', 0, 'Description', NULL, NULL, NULL, 3.50, NULL, NULL, NULL, NULL, NULL, '', 'N', 'N', 'uploads/PN416_1.jpg', 'uploads/nopic.jpg', 'uploads/nopic.jpg', 'uploads/nopic.jpg', '/uploads/nopic.jpg', NULL, 0, 3),
(418, 'S', NULL, 'Van butter', '0', '0', NULL, '', 'NL', 'Vanilla butterfly with jam centre', 'Our butterfly vanilla cupcake with vanilla icing and jam centre', NULL, NULL, 0.00, 0, 0, 0, 1, NULL, NULL, 'Regular Parcel - $6.60    Express Post - $9.55', 0, 'Description', NULL, NULL, NULL, 3.50, NULL, NULL, NULL, NULL, NULL, '', 'N', 'N', 'uploads/PN418_1.jpg', 'uploads/nopic.jpg', 'uploads/nopic.jpg', 'uploads/nopic.jpg', '/uploads/nopic.jpg', NULL, 0, 3),
(433, 'S', NULL, 'Jaffa', '0', '0', NULL, '', 'NL', 'Jaffa squash cupcake', 'Our vanilla or chocolate cupcakes topped with vanilla icing and squashed jaffas and drizzled with chocolate sauce', NULL, NULL, 0.00, 0, 0, 0, 1, NULL, NULL, 'Regular Parcel - $6.60    Express Post - $9.55', 0, 'Description', NULL, NULL, NULL, 3.50, NULL, NULL, 'Vanilla, Chocolate', 'Flavour', NULL, '', 'N', 'N', 'uploads/PN433_1.jpg', 'uploads/nopic.jpg', 'uploads/nopic.jpg', 'uploads/nopic.jpg', '/uploads/nopic.jpg', NULL, 0, 3);
Join the community to see this answer!
Join our exclusive community to see this answer & millions of others.
Unlock 1 Answer and 5 Comments.
Join the Community
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 5 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros