I have two tables, omc_categories and omc_products.
I want to pull out categories name where omc_proudcts.category_id is equal to omc_categories.id.
I created the following sql but I am not sure which is right one.
SELECT C.Name AS CatName
FROM omc_categories AS C
LEFT JOIN omc_products AS P
ON C.id = P.category_id
WHERE P.category_id = $category_id
AND p.status = "active"
or
SELECT C.Name AS CatName
FROM omc_products AS P
LEFT JOIN omc_categories AS C
ON C.id = P.category_id
WHERE P.category_id = $category_id
AND p.status = "active"
Can anyone tell me which one right (if there is any) and why please. I am confused with join.
CREATE TABLE IF NOT EXISTS `omc_categories` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(255) NOT NULL,
`shortdesc` varchar(255) NOT NULL,
`longdesc` text NOT NULL,
`status` enum('active','inactive') NOT NULL,
`parentid` int(11) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=43 ;
and
CREATE TABLE IF NOT EXISTS `omc_products` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(255) NOT NULL,
`shortdesc` varchar(255) NOT NULL,
`longdesc` text NOT NULL,
`thumbnail` varchar(255) NOT NULL,
`image` varchar(255) NOT NULL,
`class` varchar(255) DEFAULT NULL,
`grouping` varchar(16) DEFAULT NULL,
`status` enum('active','inactive') NOT NULL,
`category_id` int(11) NOT NULL,
`featured` enum('true','false') NOT NULL,
`price` float(4,2) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=74 ;
--UPDATE--
Sample data of omc_categories
INSERT INTO `omc_categories` (`id`, `name`, `shortdesc`, `longdesc`, `status`, `parentid`) VALUES
(21, 'Front top', 'front top', '', 'active', 4),
(20, 'Galleri 2', 'Galleri 2', '', 'active', 4),
...
(41, 'Trær', '', '', 'active', 27),
(42, 'newfolder', '', '', 'active', 27);
Sample data of omc_products
INSERT INTO `omc_products` (`id`, `name`, `shortdesc`, `longdesc`, `thumbnail`, `image`, `class`, `grouping`, `status`, `category_id`, `featured`, `price`) VALUES
(1, 'Doggie', 'Cappelen forlag: New Flight', 'Doggie from New flight.', 'images/newflight_doggie_small.jpg', 'images/newflight_doggie_big.jpg', 'new-flight', 'imagebox-new', 'active', 5, 'false', 0.00),
(2, 'Jinnie', 'New flight Jinnie', '', 'images/newflight_jinnie_small.jpg', 'images/newflight_jinnie_big1.jpg', 'new-flight', 'imagebox-new', 'active', 5, 'false', 0.00),
...
...
(73, 'new image', '', '', 'images/daffodil_big.jpg.jpg', 'images/daffodil_big.jpg', '', '', 'active', 42, 'false', 0.00);
For example last line id 73 has 42 of category_id.
I want to pull out name of category "newfolder" from omc_categories.