简体   繁体   English

如果参数为空,则sql查询返回0(基于Open Office)

[英]sql query returns 0 if parameter is null (Open Office base)

I have a table "pupils" consisting of the following coloumns: 我有一个表“学生”,它由以下几行组成:

int      IDpupil
varchar  name
int      class

where class is a foreign key and not nullable, ie every pupil has to be in a class. 其中class是一个外键并且不能为空,即每个学生都必须上一堂课。

I'd like to see all pupils in a class depending on a class number I want to specify each time new. 我希望看到每班的所有学生,具体取决于我想在每次新生时指定的班级编号。

The following statement does the intended if I specify a class: 如果指定了一个类,以下语句将达到预期目的:

SELECT "IDpupil", 
       "name", 
       "class" 
  FROM "pupils" 
 WHERE (("class" = :qClass) 
        OR (:qClass is null))

But if I want to see all pupils in that table and therefore do not specify a number it returns only the pupils of class "0". 但是,如果我想查看该表中的所有学生,因此不指定数字,则仅返回“ 0”类的学生。 If I do this with names it works. 如果我用名字来做到这一点,那就行了。 It seems to me a null parameter gets cast to 0, what I don't want. 在我看来,空参数被强制转换为0,这是我不想要的。 How can I fix this? 我怎样才能解决这个问题?

I tried to use a case statement, which should test wether the input is Null or "", but I didn't get it to work. 我尝试使用一个case语句,该语句应该测试输入是否为Null或“”,但是我没有使它起作用。 Maybe also for the complexity of a case statement. 也许还因为案件陈述的复杂性。 Then I tried this link from here . 然后我从这里尝试了此链接 Didn't help either. 也没有帮助。

i cant see where exactely is your probleme but if you want to desplay all puils just a big select wihtout any WHERE close 我不能看到exactely是你probleme,但如果你想desplay所有puils只是一个大的select wihtout任何WHERE接近

SELECT "IDpupil", 
       "name", 
       "class" 
  FROM "pupils" 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM