Jump to content

Multiple related table fields as result columns (cross-tabs?)


zq29

Recommended Posts

I'm trying to write a query that returns all of the related rows from one table as columns/fields in the result set of a table join. I'm not sure if that makes sense or not, but hopefully my schema below and the result I am trying to achieve can explain it better:

 

 

CREATE TABLE `case_study` (
  `id` int( NOT NULL auto_increment,
  `title` varchar(160) NOT NULL,
  PRIMARY KEY  (`id`)
) ENGINE=MyISAM;

CREATE TABLE `case_study_header` (
  `id` int(3) NOT NULL auto_increment,
  `name` varchar(160) NOT NULL,
  PRIMARY KEY  (`id`)
) ENGINE=MyISAM ;

CREATE TABLE `case_study_content` (
  `id` int( NOT NULL auto_increment,
  `case_study` int( NOT NULL,
  `header` int(3) NOT NULL,
  `content` text NOT NULL,
  PRIMARY KEY  (`id`)
) ENGINE=MyISAM ;

INSERT INTO `case_study_header` (`id`,`name`) VALUES ('1','Header 1'), ('2','Header 2'), ('3','Header 3');

INSERT INTO `case_study` (`id`,`title`) VALUES ('1','My Case Study');

INSERT INTO `case_study_content` (`id`,`case_study`,`header`,`content`) VALUES ('1','1','1','Some content'), ('2','1','3','Some more content');

 

Trying to get the result:

 

idtitleHeader 1Header 2Header 3

1My Case StudySome contentNULLSome more content

 

I have tried referencing the case_study_content table multiple times in the query with different aliases, but that both didn't work, and wouldn't really be too great as there could be changes in the case_study_header table in the future and the query would need to be manually changed.

 

I feel it might have something to do with sub queries, unions or something of that nature?

 

Does anyone understand what I am trying to achieve, and have you any input?

Link to comment
Share on other sites

fenway, have you worked with cross-tabs in this fashion before? All of the information I can find demonstrate using cross-tabs to count data (SUM(IF(`x`=`y`,1,0)) as `foo`), not to return rows of text.

 

My attempt at getting it to return text is as follows:

SELECT csc.`case_study`, c.`title`

, IF(csc.`header` = 1, `content`,'') as `summary`
, IF(csc.`header` = 2, `content`,'') as `background`
, IF(csc.`header` = 3, `content`,'') as `approach`

FROM `case_study` as c INNER JOIN `case_study_content` as csc ON c.`id`=csc.`case_study`
GROUP BY c.`id`

 

...which returns the first bit of content (summary), but the other two fields empty.

 

idtitlesummarybackgroundappraoch

1My Case StudySome content[/td][td]

 

Instead of:

idtitlesummarybackgroundapproach

1My Case StudySome contentNULLSome more content

Link to comment
Share on other sites

Yeah, I see that now... obviously, you can tell that csc.header can only be one value at a time!

 

You'll need to join in the header table once per header row entry value with the name in the on clause -- let me know if I've lost you.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.