简体   繁体   中英

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.

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". If I do this with names it works. It seems to me a null parameter gets cast to 0, what I don't want. 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. 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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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