gpt4 book ai didi

mysql - 与复杂的查询相反?

转载 作者:行者123 更新时间:2023-11-29 11:36:00 25 4
gpt4 key购买 nike

Query Image - Table Structure

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 的词汇表时,它应该列出连接器表中未连接的动词。如果第二个词汇是“你的车”,则不应列出“修理、清洁、使用、清洗”,而必须列出“吃、切片、喝”。

最佳答案

对于这样的问题,我从我要寻找的动词开始。

select * from verbs where ...

您提到您可以构建一个查询,通过连接表来查找与词汇表 2 关联的动词。

select verbid from verbvocabconnector 
where verbvocabconnector.VOCABID = 2

将这两者结合起来,您需要此查询未返回的动词。只要您只返回动词 id,您就可以选择具有 id 的动词,或者在本例中,不在该子查询选择中选择动词

select * from verbs
where id not in (subquery);

关于mysql - 与复杂的查询相反?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36584888/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com