查询多个联结表

时间:2014-10-15 14:48:54

标签: mysql sql create-table

我有一个带有多个联结表的MySQL数据库,因为每个案例都可以链接到多个组织,结果和问题。所以我为每个多对多关系都有一个联结表。

我的问题是如何撰写一份声明,将所有这些信息巧妙地整合到每个案例研究的一行中。我之前的问题给了我使用一个联结表的解决方案,但现在我有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

我试图使用两个联结表是不行的。我尝试了UNION的结果好坏参与。

感谢任何帮助

以下是创建语句

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`);

0 个答案:

没有答案