Re: Trying to get categories and relational table information in one query
Date: May 31, 2012 12:32AM
CREATE TABLE IF NOT EXISTS `blog_categories` (
`id` int(12) NOT NULL AUTO_INCREMENT,
`label` varchar(45) NOT NULL,
`long_label` varchar(255) DEFAULT NULL,
`description` text,
`createDate` datetime NOT NULL,
`modBy` varchar(255) NOT NULL,
`modDate` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
`sortorder` int(12) NOT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `label_UNIQUE` (`label`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=4 ;
CREATE TABLE IF NOT EXISTS `blog_post2cat` (
`id` int(12) NOT NULL AUTO_INCREMENT,
`idPost` int(12) NOT NULL,
`idCat` int(12) NOT NULL,
PRIMARY KEY (`id`),
KEY `fk_blog_post2cat_blog_categories1` (`idCat`),
KEY `fk_blog_post2cat_blog_posts` (`idPost`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ;
So if I have a key value for idPost and idCat is a foreign key for the categories table, can I generate a single SQL statement that would output every row of the categories table with an alias field (boolean output would be fine) that would indicate which categories have a record in the blog_post2cat table connecting a specific article (specific idPost value) to each category?
e.g. 0 = this article is not assigned to this category and 1 = this article is assigned to this category.
What is tripping me up is that I want all the rows of the categories table and not just the rows that have a record in the relational table.