Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/56.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
MySQL连接查询语句_Mysql_Join - Fatal编程技术网

MySQL连接查询语句

MySQL连接查询语句,mysql,join,Mysql,Join,我有三张桌子 我需要一些帮助来组合一个连接查询 表1:工程 这是我的主桌。 它包含许多不同的字段,包括一个唯一的主键。 它还包含一个“composers_id”字段,该字段与下一个表相关 表2:作曲家 此表包含有关“作品”表中作品的作曲家的其他信息。 它还包含许多字段,包括一个唯一的主键——这是与“工作”相关的id 表3:工程和仪器 此表包含工程中仪器的其他信息。 它包含唯一的主键。 并且它包含一个“works_id”字段,该字段与“works”表中的唯一id相关。 有几行都与每个“作品id”有

我有三张桌子

我需要一些帮助来组合一个连接查询

表1:工程 这是我的主桌。 它包含许多不同的字段,包括一个唯一的主键。 它还包含一个“composers_id”字段,该字段与下一个表相关

表2:作曲家 此表包含有关“作品”表中作品的作曲家的其他信息。 它还包含许多字段,包括一个唯一的主键——这是与“工作”相关的id

表3:工程和仪器 此表包含工程中仪器的其他信息。 它包含唯一的主键。 并且它包含一个“works_id”字段,该字段与“works”表中的唯一id相关。 有几行都与每个“作品id”有关,因为在每一部作品中都有很多关于每一种乐器的信息

交易如下: 我需要能够在works表中搜索结果,但只能搜索符合条件的结果,这些条件只能在works表中检查

我是这样想的:

"SELECT
    works.id,
    works.title

FROM works

    JOIN composers

        ON works.composers_id = composers.id"
"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"
但是,如果我只需要从works表中获得结果——如果相关“works\u id”的一行中有一个长笛,该怎么办? 除了“works\u id”字段外,“works\u instruments”还包含一个“instrument”字段,该字段可能包含也可能不包含单词“fleet”。 将有许多行具有相同的“works\u id”。例如:在“乐器”字段中,1行代表“长笛”,在“乐器”字段中,另一行代表“低音”等等

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

你能帮忙吗

先谢谢你 约翰

编辑:

这是工作表:

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
这是Composer表:

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_仪器表:

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”是我以前称之为“instrument”)

答案比我想象的要简单

我刚刚添加了另一个连接,以包含具有正确works\u id的works\u仪器 然后是WHERE子句,以确保包含正确的工具

看起来是这样的:

"SELECT
    works.id,
    works.title

FROM works

    JOIN composers

        ON works.composers_id = composers.id"
"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"
这似乎工作得很好

我很抱歉浪费了你的时间,事实上我自己也能做到。
我想我只是不相信自己能做到这一点。:-)

works\u instruments
定义得很好,还是不规范?
instruments
表在哪里,因此
wi
表是一个连接表概念我怀疑这个问题可能会更冗长,但我不确定如何解决。我建议您为每个xxx显示类似于
show create table xxx
的模式,所以peeps可以在你塞满东西之前对它进行友好的检查。彻底地。密切投票记录在案。开始打字我不太明白你的问题,德鲁?但是,是的,我确实相信works表定义得很好。仪器表被称为works\u instruments。。。。这就是你要问的吗?你是说桌子结构乱七八糟?