Skip navigation links

MySQL Forums :: PHP :: Trying to get categories and relational table information in one query


Advanced Search

Re: Trying to get categories and relational table information in one query
Posted by: Steven B ()
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.

Options: ReplyQuote


Subject Written By Posted
Trying to get categories and relational table information in one query Steven B 05/30/2012 05:03PM
Re: Trying to get categories and relational table information in one query Peter Brawley 05/30/2012 08:20PM
Re: Trying to get categories and relational table information in one query Steven B 05/31/2012 12:32AM
Re: Trying to get categories and relational table information in one query laptop alias 05/31/2012 01:52AM
Re: Trying to get categories and relational table information in one query Steven B 05/31/2012 02:43AM
Re: Trying to get categories and relational table information in one query laptop alias 05/31/2012 03:47AM
Re: Trying to get categories and relational table information in one query Steven B 05/31/2012 03:56PM
Re: Trying to get categories and relational table information in one query laptop alias 05/31/2012 05:21PM
Re: Trying to get categories and relational table information in one query Steven B 05/31/2012 06:34PM
Re: Trying to get categories and relational table information in one query laptop alias 06/01/2012 01:19AM
Re: Trying to get categories and relational table information in one query Steven B 06/01/2012 03:50AM
Re: Trying to get categories and relational table information in one query laptop alias 06/01/2012 07:28AM
Re: Trying to get categories and relational table information in one query Steven B 06/02/2012 01:13AM
Re: Trying to get categories and relational table information in one query Barry Galbraith 06/01/2012 06:52PM


Sorry, you can't reply to this topic. It has been closed.

Content reproduced on this site is the property of the respective copyright holders. It is not reviewed in advance by Oracle and does not necessarily represent the opinion of Oracle or any other party.