Select
random.verbvocabconnector.id,
random.verbvocabconnector.verbid,
random.verbvocabconnector.vocabid,
random.verbs.id As id1,
random.verbs.verb,
random.vocabulary.id As id2,
random.vocabulary.vocabulary
From
random.verbvocabconnector Inner Join
random.vocabulary
On random.vocabulary.id = random.verbvocabconnector.vocabid Inner Join
random.verbs
On random.verbvocabconnector.verbid = random.verbs.id
Where
random.vocabulary.id = 2
通过使用变量 vocabid ,我可以列出与词汇表相关的所有动词。但我想做相反的事情,如果id为2则不要显示与该词汇表相关的动词并显示所有其他动词。您可以在图像上看到详细的表格结构。提前谢谢。
编辑:我有这些表:动词,词汇,verbandvocabconnector。
CREATE TABLE `verbs` (
`id` int(11) NOT NULL,
`verb` text NOT NULL,
`regularorirregular` int(11) NOT NULL,
`irregular` text NOT NULL,
`irregular2` text NOT NULL
) ENGINE=MyISAM DEFAULT CHARSET=latin1;
INSERT INTO `verbs` (`id`, `verb`, `regularorirregular`, `irregular`, `irregular2`)
VALUES
(1, 'eat', 1, 'ate', 'eaten'),
(2, 'repair', 0, '', ''),
(3, 'clean', 0, '', ''),
(4, 'use', 1, 'used', 'used'),
(8, 'slice', 1, 'sliced', 'sliced'),
(6, 'drink', 1, 'drank', 'drunk'),
(7, 'wash', 0, '', '');
CREATE TABLE `vocabulary` (
`id` int(11) NOT NULL,
`vocabulary` text NOT NULL,
`category` text NOT NULL
) ENGINE=MyISAM DEFAULT CHARSET=latin1;
INSERT INTO `vocabulary` (`id`, `vocabulary`, `category`)
VALUES
(1, 'apple', 'fruits'),
(2, 'your car', 'object'),
(3, 'his toothbrush', 'object'),
(4, 'banana', 'fruits'),
(5, 'our dishwasher', 'object'),
(6, 'tea', 'drinks'),
(7, 'soda', 'drinks'),
(8, 'fruit juice', 'drinks');
CREATE TABLE `verbvocabconnector` (
`id` int(11) NOT NULL,
`verbid` int(11) NOT NULL,
`vocabid` int(11) NOT NULL
) ENGINE=MyISAM DEFAULT CHARSET=latin1;
INSERT INTO `verbvocabconnector` (`id`, `verbid`, `vocabid`)
VALUES
(1, 1, 1),
(2, 1, 4),
(3, 2, 2),
(4, 2, 5),
(6, 3, 2),
(7, 3, 3),
(8, 3, 5),
(9, 4, 2),
(10, 4, 3),
(11, 4, 5),
(13, 5, 2),
(14, 5, 5),
(15, 6, 6),
(16, 6, 7),
(17, 6, 8),
(18, 7, 2),
(19, 7, 1),
(20, 7, 3),
(21, 8, 1),
(22, 8, 4);
我希望从这些表中得到的输出;当我选择id = 2的词汇表时,它应该列出连接器表中未连接的动词。如果第二个词汇是&#34;你的车&#34;它不应该列出&#34;修复,清洁,使用,清洗&#34; ,并且必须列出&#34;吃,切片,喝。&#34; < / p>
答案 0 :(得分:1)
对于这样的问题,我从我正在寻找的动词开始。
select * from verbs where ...
您提到可以通过连接表来构建查询以查找与词汇表2关联的动词。
select verbid from verbvocabconnector
where verbvocabconnector.VOCABID = 2
结合这两个,您需要此查询未返回的动词。只要您只返回动词ID,就可以选择包含id的动词,或者在这种情况下,不在该子查询选择中
select * from verbs
where id not in (subquery);