2015-11-03 108 views
0

我有3个表格。MySQL JOIN查询语句

而且我可以使用一些帮助放在一起JOIN查询。

表1:作品 这是我的主表。它包含很多不同的字段,包括一个唯一的主键。 它包含一个“composers_id”字段 - 涉及下一个表格。

表2:作曲家 此表包含关于“作品”表中作品的作曲家的其他信息。它包含许多领域,包括一个独特的主键 - 这是“工作”涉及到的ID。

表3:works_instruments 此表包含有关作品中的乐器的其他信息。 它包含唯一的主键。 它包含一个“works_id”字段,它与“作品”表中的唯一ID相关。 有几行都与每个“works_id”有关 - 因为每个工作中都有关于每个乐器的大量信息。

以下是处理: 我需要能够在工作表中搜索结果,但只能匹配只能在works_instruments表中检查的条件的结果。

我想是这样的:

"SELECT 
    works.id, 
    works.title 

FROM works 

    JOIN composers 

     ON works.composers_id = composers.id" 

那么是什么,如果我只需要从作品表得到的结果 - 如果在一个相关的行“works_id” S长笛? “works_instruments”除“works_id”字段外还包含一个“乐器”字段,该字段可能并可能不包含“长笛”一词。 将有许多行具有相同的“works_id”。例如:在“乐器”栏中有1行为“长笛”,在“乐器”栏中有另一行为“低音”。 - 等等。

如何进行查询,以便只从作品表中获得作品,如果涉及到长笛?

你能帮忙吗?

预先感谢您 约翰

编辑:

这是作品表:

CREATE TABLE `works` (
`id` smallint(4) unsigned NOT NULL AUTO_INCREMENT, 
`composers_id` smallint(4) unsigned NOT NULL, 
`work_no` tinytext NOT NULL, 
`composer_no` tinytext NOT NULL, 
`composers_full_name` tinytext, 
`full_title` tinytext NOT NULL, 
`short_title` tinytext NOT NULL, 
`alternative_titles` tinytext NOT NULL, 
`remarks` text NOT NULL, 
`contents` text NOT NULL, 
`first_line` tinytext NOT NULL, 
`full_inst_ed4_rev4` text NOT NULL, 
`duration` tinytext NOT NULL, 
`sourcetext` text NOT NULL, 
`chor_acc_size` tinytext NOT NULL, 
`youth_commentary` text NOT NULL, 
`youth_category_rev` tinytext NOT NULL, 
`orchestra_type` tinytext NOT NULL, 
`creation_date` date DEFAULT NULL, 
`master_title_opas` tinytext NOT NULL, 
`short_name` tinytext NOT NULL, 
`url` tinytext, 
PRIMARY KEY (`id`), 
KEY `composers_id` (`composers_id`), 
KEY `url` (`url`(16)), 
FULLTEXT KEY `keywords` (`composers_full_name`,`full_title`) 
) ENGINE=MyISAM AUTO_INCREMENT=8183 DEFAULT CHARSET=utf8 

这是作曲家表:

CREATE TABLE `composers` (
`id` smallint(4) unsigned NOT NULL AUTO_INCREMENT, 
`composer_no` tinytext NOT NULL, 
`name` tinytext NOT NULL, 
`short_name` tinytext NOT NULL, 
`birth_year` tinytext NOT NULL, 
`death_year` tinytext NOT NULL, 
`vitae` tinytext NOT NULL, 
`catalog` text NOT NULL, 
`classification` tinytext NOT NULL, 
`comment` text NOT NULL, 
`gender` tinytext NOT NULL, 
`alerts` tinytext NOT NULL, 
`corrected` tinytext NOT NULL, 
`creation_date` date DEFAULT NULL, 
`edition` tinytext NOT NULL, 
`first_name` tinytext NOT NULL, 
`full_name` tinytext NOT NULL, 
`last_name` tinytext NOT NULL, 
`modification_date` tinytext NOT NULL, 
`name_accented` tinytext NOT NULL, 
`short_name_accented` tinytext NOT NULL, 
`vitae_accented` tinytext NOT NULL, 
`url` tinytext, 
PRIMARY KEY (`id`), 
    UNIQUE KEY `url` (`url`(16)) 
    ) ENGINE=MyISAM AUTO_ 

INCREMENT=1197 DEFAULT CHARSET=utf8 

这是works_instruments表格:

CREATE TABLE `works_instruments` (
`works_id` smallint(4) unsigned NOT NULL, 
`instruments_id` smallint(4) unsigned NOT NULL, 
`number` tinyint(3) unsigned NOT NULL, 
`numbertext` tinytext NOT NULL, 
`detail` tinytext NOT NULL, 
`printout` tinytext NOT NULL, 
`editable` tinytext NOT NULL, 
UNIQUE KEY `works_instruments` (`works_id`,`instruments_id`), 
KEY `works_id` (`works_id`), 
KEY `instruments_id` (`instruments_id`), 
KEY `number` (`number`) 
) ENGINE=MyISAM DEFAULT CHARSET=utf8 

( “instruments_id” 是我以前被称为 “乐器”)

+0

是'works_instruments'很好的定义,还是它是一个非标准化的混乱? '仪表'在哪里,所以'wi'表是一个结点表概念 – Drew

+2

我怀疑这个问题可能会更加冗长,但我不知道如何。 – Strawberry

+0

我建议你用每个xxx的'show create table xxx'来显示你的模式,所以在你充满东西之前,peeps可以给它一个友好的审查。完了,走吧。关闭投票记录。开始输入 – Drew

回答

0

答案是比我想象的简单。

我刚刚添加了另一个JOIN,将work_instruments包含在正确的works_id 中,然后再加上WHERE子句,以确保包含正确的乐器。

因此,它看起来是这样的:

"SELECT 
    works.id, 
    works.title 

FROM works 

    JOIN composers 

     ON works.composers_id = composers.id 

    JOIN works_instruments 

     ON works.id = works_instruments.works_id 

WHERE works_instruments.instrument_id = the instruemnt id I am asking for" 

这似乎是工作得很好。

我很抱歉浪费你的时间,实际上能够自己做。 我想我只是不相信自己能够做到。 :-)