首页 \ 问答 \ “不在”不按预期工作(“NOT IN” not working as expected)

“不在”不按预期工作(“NOT IN” not working as expected)

我正在写一个查询来查找表BST中由节点的值排序的节点类型。 表BST有两列N和P,其中N代表BST中节点的值,P代表N的父亲。例如,BST具有以下记录: BST表

我成功执行了如下查询:

SELECT n,CASE
                WHEN p IS NULL THEN 'Root'
                WHEN n IN (SELECT DISTINCT p FROM BST) THEN 'Inner'
                ELSE 'Leaf'
             END
FROM BST
ORDER BY n;

结果: 按预期结果

但是,当我使用“NOT IN”尝试相同的查询时,不是使用“IN”,如下所示:

SELECT n,CASE
            WHEN p IS NULL THEN 'Root'
            WHEN n NOT IN (SELECT DISTINCT p FROM BST) THEN 'Leaf'
            ELSE 'Inner'
         END
FROM BST
ORDER BY n;

它没有按预期工作。 为什么这样?


I was writing a query to find the node type from the table BST ordered by the value of the node. table, BST, had two columns N and P, where N represents the value of a node in BST, and P is the parent of N. say, BST has following records: BST Table

I successfully executed the query as follows:

SELECT n,CASE
                WHEN p IS NULL THEN 'Root'
                WHEN n IN (SELECT DISTINCT p FROM BST) THEN 'Inner'
                ELSE 'Leaf'
             END
FROM BST
ORDER BY n;

Result: Result as expected

But instead of using "IN", when I tried the same query using "NOT IN" as given below:

SELECT n,CASE
            WHEN p IS NULL THEN 'Root'
            WHEN n NOT IN (SELECT DISTINCT p FROM BST) THEN 'Leaf'
            ELSE 'Inner'
         END
FROM BST
ORDER BY n;

it didn't work as expected. Why so?


原文:https://stackoverflow.com/questions/42837073
更新时间:2024-02-20 06:02

最满意答案

您应该使用'sc.nextLine()'来扫描字符串值,您应该使用'sc.nextInt'来扫描整数值。 如果在键入一堆代码时按ctrl和space键,它会显示您可能要编写的内容。


You should use 'sc.nextLine()' to scan string values, and you should use 'sc.nextInt' to scan integer values. If you press ctrl and space keys while typing a bunch of code it shows you the possible things you may want to write.

相关问答

更多
  • 静态进口做的诀窍: import static java.lang.System.out; 或者使用导入每个静态方法和字段 import static java.lang.System.*; @Steve C的附录:请注意,@sfussenegger在对我的答案的评论中表示。 “使用这样一个静态导入的System.out不适用于简单的一次运行代码。” 所以请不要以为他(或我)认为这个解决方案是好的做法。 static imports do the trick: import static java.la ...
  • 我声明扫描仪的方式是错误的,还是我不能使用与扫描仪配合使用的映射? Scanner声明似乎是正确的。 不,没有限制禁止同时使用Java标准库的任何两个部分。 所以一起使用Map和Scanner是完全可以的。 目前,SO社区的最佳猜测是您正在使用具有内置控制台窗口/视图的IDE(如eclipse)。 在此假设下,假设您希望打开黑色终端/ cmd窗口,但在大多数IDE中,情况并非如此。 在eclipse中,“控制台视图”是您进行输入的地方。 在Netbeans中,这将是输出窗口。 Is the way in w ...
  • 你的代码在下面一行生成一个IndexOutOfBoundsException并跳转到异常处理程序代码。 String firstName=custInfo[counter].substring(0,(' ')); String.substring重载并有两个定义: substring(int startIndex) substring(int startIndex, int endIndex) 在Java中, char数据类型仅仅是幕后的一个16位数字。 它会将' ' (空格)转换为32,并且会在比此短 ...
  • 您应该使用try-with-resouces关闭扫描仪。 import java.io.File; import java.io.FileNotFoundException; import java.io.PrintWriter; import java.util.Scanner; public class Characters { public static void main(String[] args) throws FileNotFoundException { Scann ...
  • 问题在于 corner = autopy.bitmap.Bitmap.open('corner.png') 文件corner.png是8位png。 不得不是32位。 Problem was in corner = autopy.bitmap.Bitmap.open('corner.png') The file corner.png was 8 bit png. Had to be 32 bit.
  • 您应该使用'sc.nextLine()'来扫描字符串值,您应该使用'sc.nextInt'来扫描整数值。 如果在键入一堆代码时按ctrl和space键,它会显示您可能要编写的内容。 You should use 'sc.nextLine()' to scan string values, and you should use 'sc.nextInt' to scan integer values. If you press ctrl and space keys while typing a bunch o ...
  • 使用want = scan.next(); 而不是nextLine() 。 你的问题的原因是,在前面的nextInt() ,你仍然在同一行, nextLine()返回当前行的其余部分。 以下是重现此行为的最小片段: Scanner sc = new Scanner(System.in); System.out.println("nextInt() = " + sc.nextInt()); System.out.println("nextLine() = " + sc.nextLine()); 当你输入5 ...
  • 在使用nextLine()之前,您需要检查是否有下一行。 出于这个原因,您应该使用常规的while()循环,而不是do / while。 由于它是一个扫描仪,用户可能会不断输入文本,您可能希望将该文本保存为字符串,并在while()检查EOF消息之外放置一个循环。 Before you use nextLine(), you need to check whether there is a next line. You should probably use a regular while() loop f ...
  • while(sc.nextInt() != 42){ ar.add(sc.nextInt()); } 您在每次迭代中读取两个整数,因此您可能正在读取循环内部的42,因此您在阅读时不会停止。 尝试将其更改为: int i = 0; while((i = sc.nextInt()) != 42){ ar.add(i); } while(sc.nextInt() != 42){ ar.add(sc.nextInt()); } You are reading two ints in e ...
  • 此代码在大多数情况下看起来都是正确的 虽然,我假设您的sc变量是通过多个输入(如您所述)相同的扫描程序对象,因此它的缓冲区可能无法清除,因为您每次使用sc.nextFloat()方法时都会假设它。 您可以通过在再次使用之前重新初始化Scanner对象来避免这种情况。 如果我没记错的话,没有办法明确清除扫描器对象的缓冲区,所以必须重新初始化它才能“清除”它。 您还可以使用sc.hasNextFloat()来验证是否已输入浮点值。 您还可以使用和Float.parseFloat(sc.nextLine())在输 ...

相关文章

更多

最新问答

更多
  • 您如何使用git diff文件,并将其应用于同一存储库的副本的本地分支?(How do you take a git diff file, and apply it to a local branch that is a copy of the same repository?)
  • 将长浮点值剪切为2个小数点并复制到字符数组(Cut Long Float Value to 2 decimal points and copy to Character Array)
  • OctoberCMS侧边栏不呈现(OctoberCMS Sidebar not rendering)
  • 页面加载后对象是否有资格进行垃圾回收?(Are objects eligible for garbage collection after the page loads?)
  • codeigniter中的语言不能按预期工作(language in codeigniter doesn' t work as expected)
  • 在计算机拍照在哪里进入
  • 使用cin.get()从c ++中的输入流中丢弃不需要的字符(Using cin.get() to discard unwanted characters from the input stream in c++)
  • No for循环将在for循环中运行。(No for loop will run inside for loop. Testing for primes)
  • 单页应用程序:页面重新加载(Single Page Application: page reload)
  • 在循环中选择具有相似模式的列名称(Selecting Column Name With Similar Pattern in a Loop)
  • System.StackOverflow错误(System.StackOverflow error)
  • KnockoutJS未在嵌套模板上应用beforeRemove和afterAdd(KnockoutJS not applying beforeRemove and afterAdd on nested templates)
  • 散列包括方法和/或嵌套属性(Hash include methods and/or nested attributes)
  • android - 如何避免使用Samsung RFS文件系统延迟/冻结?(android - how to avoid lag/freezes with Samsung RFS filesystem?)
  • TensorFlow:基于索引列表创建新张量(TensorFlow: Create a new tensor based on list of indices)
  • 企业安全培训的各项内容
  • 错误:RPC失败;(error: RPC failed; curl transfer closed with outstanding read data remaining)
  • C#类名中允许哪些字符?(What characters are allowed in C# class name?)
  • NumPy:将int64值存储在np.array中并使用dtype float64并将其转换回整数是否安全?(NumPy: Is it safe to store an int64 value in an np.array with dtype float64 and later convert it back to integer?)
  • 注销后如何隐藏导航portlet?(How to hide navigation portlet after logout?)
  • 将多个行和可变行移动到列(moving multiple and variable rows to columns)
  • 提交表单时忽略基础href,而不使用Javascript(ignore base href when submitting form, without using Javascript)
  • 对setOnInfoWindowClickListener的意图(Intent on setOnInfoWindowClickListener)
  • Angular $资源不会改变方法(Angular $resource doesn't change method)
  • 在Angular 5中不是一个函数(is not a function in Angular 5)
  • 如何配置Composite C1以将.m和桌面作为同一站点提供服务(How to configure Composite C1 to serve .m and desktop as the same site)
  • 不适用:悬停在悬停时:在元素之前[复制](Don't apply :hover when hovering on :before element [duplicate])
  • 常见的python rpc和cli接口(Common python rpc and cli interface)
  • Mysql DB单个字段匹配多个其他字段(Mysql DB single field matching to multiple other fields)
  • 产品页面上的Magento Up出售对齐问题(Magento Up sell alignment issue on the products page)