草庐IT

转换工作SQL不在

程序员大本营 2025-04-25 原文

我有一些SQL告诉我从未挥舞过剑的超级英雄。

创建表

首先,这是(MySQL)脚本来创建和填充表。

CREATE TABLE IF NOT EXISTS `mydb`.`weaponry` (  `hero` VARCHAR(20) NULL,  `weapon` VARCHAR(20) NULL)ENGINE = InnoDBinsert into weaponry (hero, weapon) values ('Wonder Woman', 'Lasso of truth');insert into weaponry (hero, weapon) values ('Wonder Woman', 'Sword');insert into weaponry (hero, weapon) values ('Wonder Woman', 'Shield');insert into weaponry (hero, weapon) values ('Spider Man', 'Web');insert into weaponry (hero, weapon) values ('Gamora', 'Sword');insert into weaponry (hero, weapon) values ('Gamora', 'Daggar');insert into weaponry (hero, weapon) values ('Batman', 'Batarang');insert into weaponry (hero, weapon) values ('Batman', 'Batgrapple');insert into weaponry (hero, weapon) values ('Batman', 'Tranquilizer gun');

使用一个工作代码 NOT IN 和一个子选择

select distinct hero from weaponry  where hero not in  (select hero from weaponry where weapon = 'Sword')

这正确返回了蜘蛛侠和蝙蝠侠,他们不挥舞剑。

替代1:不存在

我想使用相同的查询 NOT EXISTS。我看着 https://stackoverflow.com/a/5231731/509840。我尝试了:

select distinct hero from weaponry  where not exists  (select * from weaponry where weapon = 'Sword')

不幸的是,这不会返回行。

替代2:使用左加入

我还看了这个答案: https://stackoverflow.com/a/17413996/509840 。我很好奇尝试更换 NOT INLEFT JOIN.

  select * from weaponry a    left join weaponry b on a.hero=b.hero and a.weapon=b.weapon    where a.weapon <> 'Sword'

但是,这只是返回所有非词英雄(包括神奇女侠和Gamora),因为除了剑以外的所有挥舞武器。

您能帮我重写与任何一个 NOT EXISTS 或者 LEFT JOIN?

编辑:左加入和相关的子查询

@Jeffuk在他的评论中建议,可以通过删除来理解相关的子查询 WHERE 条款。这是对 selectwhere 删除。

select distinct a.hero, weaponryInner.hero from weaponry a    left join         (Select * from weaponry b where weapon = 'Sword') as weaponryInner    on a.hero=weaponryInner.hero 

结果是两列英雄,其中一些是 NULL.

-------------------------------Hero          |  Hero-------------------------------Wonder Woman  |  Wonder WomanGamora        |  GamoraSpider Man    |  <null> Batman        |  <null>-------------------------------

因此添加决赛 where weaponryInner.Weapon is null 将返回蜘蛛侠和蝙蝠侠。

看答案

首先,您要检查是否存在任何挥舞着英雄的剑,您只需要检查挥舞剑的“当前”英雄。

select distinct hero from weaponry as a  where not exists  (select * from weaponry as b where b.weapon = 'Sword' and b.hero = a.hero )

第二个对您不起作用,每行都可以在每行上进行连接作品,因此可以准确地完成您所看到的。您可以做这样的事情,有点复杂,但要完成工作。

我们只在子查询中发现了带有剑的英雄,并将其加入所有的英雄,任何带有“无效”的英雄都没有在子查询中发现,因此永远不会挥舞剑。

select distinct a.hero from weaponry a    left join         (Select * from weaponry b where weapon = 'Sword') as weaponryInner    on a.hero=weaponryInner.hero where weaponryInner.Weapon is null

有关转换工作SQL不在的更多相关文章

  1. ruby-on-rails - 在 Rails 中将文件大小字符串转换为等效千字节 - 2

    我的目标是转换表单输入,例如“100兆字节”或“1GB”,并将其转换为我可以存储在数据库中的文件大小(以千字节为单位)。目前,我有这个:defquota_convert@regex=/([0-9]+)(.*)s/@sizes=%w{kilobytemegabytegigabyte}m=self.quota.match(@regex)if@sizes.include?m[2]eval("self.quota=#{m[1]}.#{m[2]}")endend这有效,但前提是输入是倍数(“gigabytes”,而不是“gigabyte”)并且由于使用了eval看起来疯狂不安全。所以,功能正常,

  2. ruby-on-rails - 由于 "wkhtmltopdf",PDFKIT 显然无法正常工作 - 2

    我在从html页面生成PDF时遇到问题。我正在使用PDFkit。在安装它的过程中,我注意到我需要wkhtmltopdf。所以我也安装了它。我做了PDFkit的文档所说的一切......现在我在尝试加载PDF时遇到了这个错误。这里是错误:commandfailed:"/usr/local/bin/wkhtmltopdf""--margin-right""0.75in""--page-size""Letter""--margin-top""0.75in""--margin-bottom""0.75in""--encoding""UTF-8""--margin-left""0.75in""-

  3. ruby-on-rails - 'compass watch' 是如何工作的/它是如何与 rails 一起使用的 - 2

    我在我的项目目录中完成了compasscreate.和compassinitrails。几个问题:我已将我的.sass文件放在public/stylesheets中。这是放置它们的正确位置吗?当我运行compasswatch时,它不会自动编译这些.sass文件。我必须手动指定文件:compasswatchpublic/stylesheets/myfile.sass等。如何让它自动运行?文件ie.css、print.css和screen.css已放在stylesheets/compiled。如何在编译后不让它们重新出现的情况下删除它们?我自己编译的.sass文件编译成compiled/t

  4. ruby - 使用 ruby​​ 将 HTML 转换为纯文本并维护结构/格式 - 2

    我想将html转换为纯文本。不过,我不想只删除标签,我想智能地保留尽可能多的格式。为插入换行符标签,检测段落并格式化它们等。输入非常简单,通常是格式良好的html(不是整个文档,只是一堆内容,通常没有anchor或图像)。我可以将几个正则表达式放在一起,让我达到80%,但我认为可能有一些现有的解决方案更智能。 最佳答案 首先,不要尝试为此使用正则表达式。很有可能你会想出一个脆弱/脆弱的解决方案,它会随着HTML的变化而崩溃,或者很难管理和维护。您可以使用Nokogiri快速解析HTML并提取文本:require'nokogiri'h

  5. ruby - 将数组的内容转换为 int - 2

    我需要读入一个包含数字列表的文件。此代码读取文件并将其放入二维数组中。现在我需要获取数组中所有数字的平均值,但我需要将数组的内容更改为int。有什么想法可以将to_i方法放在哪里吗?ClassTerraindefinitializefile_name@input=IO.readlines(file_name)#readinfile@size=@input[0].to_i@land=[@size]x=1whilex 最佳答案 只需将数组映射为整数:@land边注如果你想得到一条线的平均值,你可以这样做:values=@input[x]

  6. ruby - 将散列转换为嵌套散列 - 2

    这道题是thisquestion的逆题.给定一个散列,每个键都有一个数组,例如{[:a,:b,:c]=>1,[:a,:b,:d]=>2,[:a,:e]=>3,[:f]=>4,}将其转换为嵌套哈希的最佳方法是什么{:a=>{:b=>{:c=>1,:d=>2},:e=>3,},:f=>4,} 最佳答案 这是一个迭代的解决方案,递归的解决方案留给读者作为练习:defconvert(h={})ret={}h.eachdo|k,v|node=retk[0..-2].each{|x|node[x]||={};node=node[x]}node[

  7. ruby-on-rails - form_for 中不在模型中的自定义字段 - 2

    我想向我的Controller传递一个参数,它是一个简单的复选框,但我不知道如何在模型的form_for中引入它,这是我的观点:{:id=>'go_finance'}do|f|%>Transferirde:para:Entrada:"input",:placeholder=>"Quantofoiganho?"%>Saída:"output",:placeholder=>"Quantofoigasto?"%>Nota:我想做一个额外的复选框,但我该怎么做,模型中没有一个对象,而是一个要检查的对象,以便在Controller中创建一个ifelse,如果没有检查,请帮助我,非常感谢,谢谢

  8. ruby - 无法让 RSpec 工作—— 'require' : cannot load such file - 2

    我花了三天的时间用头撞墙,试图弄清楚为什么简单的“rake”不能通过我的规范文件。如果您遇到这种情况:任何文件夹路径中都不要有空格!。严重地。事实上,从现在开始,您命名的任何内容都没有空格。这是我的控制台输出:(在/Users/*****/Desktop/LearningRuby/learn_ruby)$rake/Users/*******/Desktop/LearningRuby/learn_ruby/00_hello/hello_spec.rb:116:in`require':cannotloadsuchfile--hello(LoadError) 最佳

  9. ruby-on-rails - rspec should have_select ('cars' , :options => ['volvo' , 'saab' ] 不工作 - 2

    关闭。这个问题需要detailsorclarity.它目前不接受答案。想改进这个问题吗?通过editingthispost添加细节并澄清问题.关闭8年前。Improvethisquestion在首页我有:汽车:VolvoSaabMercedesAudistatic_pages_spec.rb中的测试代码:it"shouldhavetherightselect"dovisithome_pathit{shouldhave_select('cars',:options=>['volvo','saab','mercedes','audi'])}end响应是rspec./spec/request

  10. ruby-on-rails - s3_direct_upload 在生产服务器中不工作 - 2

    在Rails4.0.2中,我使用s3_direct_upload和aws-sdkgems直接为s3存储桶上传文件。在开发环境中它工作正常,但在生产环境中它会抛出如下错误,ActionView::Template::Error(noimplicitconversionofnilintoString)在View中,create_cv_url,:id=>"s3_uploader",:key=>"cv_uploads/{unique_id}/${filename}",:key_starts_with=>"cv_uploads/",:callback_param=>"cv[direct_uplo

随机推荐