“不在”不按预期工作(“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
最满意答案
您应该使用'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 ...
-
Java扫描程序无法运行(Java scanner not working)[2023-05-27]
我声明扫描仪的方式是错误的,还是我不能使用与扫描仪配合使用的映射? Scanner声明似乎是正确的。 不,没有限制禁止同时使用Java标准库的任何两个部分。 所以一起使用Map和Scanner是完全可以的。 目前,SO社区的最佳猜测是您正在使用具有内置控制台窗口/视图的IDE(如eclipse)。 在此假设下,假设您希望打开黑色终端/ cmd窗口,但在大多数IDE中,情况并非如此。 在eclipse中,“控制台视图”是您进行输入的地方。 在Netbeans中,这将是输出窗口。 Is the way in w ... -
为什么我的println不工作?(Why isn't my println working?)[2022-07-07]
你的代码在下面一行生成一个IndexOutOfBoundsException并跳转到异常处理程序代码。 String firstName=custInfo[counter].substring(0,(' ')); String.substring重载并有两个定义: substring(int startIndex) substring(int startIndex, int endIndex) 在Java中, char数据类型仅仅是幕后的一个16位数字。 它会将' ' (空格)转换为32,并且会在比此短 ... -
何时关闭扫描仪?(When should you close a scanner?)[2023-01-03]
您应该使用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 ... -
Python随机停止工作(Python randomly stops working)[2022-12-20]
问题在于 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 ...
-
Java中的扫描仪无法正常工作(Scanner in Java not working)[2022-06-10]
使用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 ...
-
扫描仪无法按预期工作(Scanner not working as expected)[2022-11-19]
此代码在大多数情况下看起来都是正确的 虽然,我假设您的sc变量是通过多个输入(如您所述)相同的扫描程序对象,因此它的缓冲区可能无法清除,因为您每次使用sc.nextFloat()方法时都会假设它。 您可以通过在再次使用之前重新初始化Scanner对象来避免这种情况。 如果我没记错的话,没有办法明确清除扫描器对象的缓冲区,所以必须重新初始化它才能“清除”它。 您还可以使用sc.hasNextFloat()来验证是否已输入浮点值。 您还可以使用和Float.parseFloat(sc.nextLine())在输 ...