0

I have a MySQL database with multiple junction tables as each case could be linked to more than one organisation, outcome and issue. So I have a junction table for each many-to-many relationship.

My question is how do I write a statement that neatly pulls all this information into a row of each case study. My previous question gave me the solution to using just one junction table but now I have 3.

SELECT   caseSummaries.caseTitle,
     caseSummaries.caseSynopsis,
caseSummaries.dateClosed,organisation.organisationName,

     GROUP_CONCAT(RESULTS.resultText)
FROM     JNCT_RESULT_CASESUMMARY
JOIN caseSummaries ON JNCT_RESULT_CASESUMMARY.caseSummary_FK = caseSummaries.caseID
JOIN     RESULTS ON JNCT_RESULT_CASESUMMARY.result_FK = RESULTS.result_ID
FROM JNCT_ORG_CASESUMMARY
JOIN    caseSummaries ON JNCT_ORG_CASESUMMARY.caseSummary_FK = caseSummaries.runningNumber
JOIN    organisation ON JNCT_ORG_CASESUMMARY.organisation_FK = organisation.organisationID
GROUP BY caseSummaries.caseTitle, caseSummaries.caseSynopsis

Which is my attempt to use two junction tables doesn't work. I tried UNION with mixed results.

Appreciate any help

Here are the create statements

CREATE TABLE IF NOT EXISTS `caseSummaries` (
`caseID` int(11) NOT NULL auto_increment,
`runningNumber` int(11) default NULL COMMENT 'Case summary number assigned',
`dateClosed` date default NULL COMMENT 'Date case was closed',
`caseTitle` varchar(250) NOT NULL COMMENT 'Title of the case summary',
`caseSynopsis` varchar(500) NOT NULL COMMENT 'Introductory paragraph',
`caseSummary` text NOT NULL COMMENT 'Main body of text',
`caseLocation` varchar(250) default NULL COMMENT 'Where the organisation is located',
`caseRegion` varchar(250) default NULL,
`caseService` int(11) NOT NULL,
PRIMARY KEY  (`caseID`),
KEY `caseService` (`caseService`)
) ENGINE=InnoDB  DEFAULT CHARSET=latin1 COMMENT='This table contains the case summaries displayed           on the webs' AUTO_INCREMENT=37 ;

-- Table structure for table `CONCERNS`
--

CREATE TABLE IF NOT EXISTS `CONCERNS` (
`concernsID` int(11) NOT NULL auto_increment,
`concern` text NOT NULL,
 PRIMARY KEY  (`concernsID`)
 ) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=11 ;

-- Table structure for table `JNCT_CONCERNS_CASESUMMARY`
--

CREATE TABLE IF NOT EXISTS `JNCT_CONCERNS_CASESUMMARY` (
`concernsCaseSummary_ID` int(11) NOT NULL auto_increment,
`concerns_FK` int(11) NOT NULL,
`caseSummary_FK` int(11) NOT NULL,
PRIMARY KEY  (`concernsCaseSummary_ID`),
KEY `concerns_FK` (`concerns_FK`,`caseSummary_FK`),
KEY `caseSummary_FK` (`caseSummary_FK`)
) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=3 ;

--
-- Constraints for dumped tables
--

--
-- Constraints for table `JNCT_CONCERNS_CASESUMMARY`
--

ALTER TABLE `JNCT_CONCERNS_CASESUMMARY`
ADD CONSTRAINT `JNCT_CONCERNS_CASESUMMARY_ibfk_1` FOREIGN KEY 
(`concerns_FK`) REFERENCES              `CONCERNS` (`concernsID`) ON UPDATE 
CASCADE,
  ADD CONSTRAINT `JNCT_CONCERNS_CASESUMMARY_ibfk_2` FOREIGN KEY 
(`caseSummary_FK`) REFERENCES `caseSummaries` (`caseID`) ON UPDATE CASCADE;

-- Table structure for table `JNCT_ORG_CASESUMMARY`
--

CREATE TABLE IF NOT EXISTS `JNCT_ORG_CASESUMMARY` (
  `org_caseSummary_ID` int(11) NOT NULL auto_increment,
  `organisation_FK` int(11) NOT NULL,
  `caseSummary_FK` int(11) NOT NULL,
  PRIMARY KEY  (`org_caseSummary_ID`),
  KEY `organisation_FK` (`organisation_FK`,`caseSummary_FK`),
  KEY `caseSummary_FK` (`caseSummary_FK`)
) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=59 ;

-- Table structure for table `organisation`
--

CREATE TABLE IF NOT EXISTS `organisation` (
  `organisationID` int(11) NOT NULL auto_increment,
  `organisationName` varchar(250) NOT NULL,
  `organisationAlias` varchar(250) default NULL COMMENT 'Commonly used name for organisation',
  `organisationParent` int(11) default NULL,
  `organisationType` int(11) NOT NULL,
  `nhsOrgID` int(11) default NULL,
  `locationID` int(11) default NULL,
  PRIMARY KEY  (`organisationID`),
  KEY `organisationParent` (`organisationParent`,`organisationType`,`nhsOrgID`,`locationID`),
  KEY `organisationType` (`organisationType`),
  KEY `nhsOrgID` (`nhsOrgID`),
  KEY `locationID` (`locationID`)
) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=628 ;

--
-- Constraints for dumped tables
--

--
-- Constraints for table `organisation`
--
ALTER TABLE `organisation`
  ADD CONSTRAINT `organisation_ibfk_7` FOREIGN KEY (`organisationType`) REFERENCES `ombudsmanService` (`serviceID`) ON DELETE CASCADE ON UPDATE CASCADE,
  ADD CONSTRAINT `organisation_ibfk_3` FOREIGN KEY (`nhsOrgID`) REFERENCES `nhsOrganisationType` (`nhsOrgID`) ON DELETE CASCADE ON UPDATE CASCADE,
  ADD CONSTRAINT `organisation_ibfk_4` FOREIGN KEY (`locationID`) REFERENCES `location` (`locationID`) ON DELETE CASCADE ON UPDATE CASCADE,
  ADD CONSTRAINT `organisation_ibfk_6` FOREIGN KEY (`organisationParent`) REFERENCES `organisation` (`organisationID`);
Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
  • If you like, consider following this simple two-step course of action: 1. If you have not already done so, provide proper DDLs (and/or an sqlfiddle) so that we can more easily replicate the problem. 2. If you have not already done so, provide a desired result set that corresponds with the information provided in step 1. – Strawberry Oct 15 '14 at 15:01
  • Not sure how to do all that but I've added my create statements to the question – Clement Oke Oct 16 '14 at 11:54
  • Have you ever tried to create a query without a single row of data? No matter how you try the most beautiful query against those barren tables will be: Nothing. There is nothing to test with. Nada. Jack Squat. Zip. Emptiness. Lonely lonely emptiness. You need a "sample" of the problem, This won;t require every field of every table and it doesn't require manyGb of data either. "Abstract" the problem to something easy to work with - this also helps you explain the issues to us. – Paul Maxwell Oct 18 '14 at 08:04
  • That was the short version of "could you add data to your table"? I've moved on to another question with a link to an sql fiddle here http://stackoverflow.com/questions/26429513/trying-to-create-one-table-from-four – Clement Oke Oct 18 '14 at 13:17
  • You cannot have two FROM clauses in a query. period. – wildplasser Dec 20 '17 at 20:45

0 Answers0