Ankündigung

Einklappen
Keine Ankündigung bisher.

Prüfen auf NULL in Where-Klausel

Einklappen

Neue Werbung 2019

Einklappen
X
  • Filter
  • Zeit
  • Anzeigen
Alles löschen
neue Beiträge

  • Prüfen auf NULL in Where-Klausel

    Ich habe folgende SELECT-Klausel:

    PHP-Code:
     $SQL_Select "SELECT t10.wpid, t10.wpname,
                                           t20.stammdatenid, t20.depotid,
                                           t30.divid
                    FROM
                        wertpapier_tab t10
                    LEFT JOIN
                        stammdaten_tab t20 ON t20.wpid = t10.wpid    
                    LEFT JOIN    
                        dividenden_tab t30 ON t30.stammdatenid = t20.stammdatenid"
    ;

     
    $SQL_Select 
    Dahingehend erhalte ich nun das folgende Array:

    [0] => array(4) { 'wpid' => string(1) "1" 'wpname' => string(9) "Aktie1" 'stammdatenid' => string(2) "14" 'divid' => string(3) "440" }
    [1] => array(4) { 'wpid' => string(1) "2" 'wpname' => string(9) "Aktie2' => string(3) "470" 'divid' => NULL }
    [2] => array(4) { 'wpid' => string(1) "3" 'wpname' => string(9) "Aktie3" 'stammdatenid' => string(2) "14" 'divid' => string(4) "2524" }


    Über eine WHERE-Klause will ich mir nun nur die Werte des Array ausgeben bei der 'divid' => NULL ist.

    PHP-Code:

    //Versuch1:
    $SQL_Where "WHERE `t30`.`divid` = '' . " ORDER BY `t10`.`wpname";
    $SQL_Where = "WHERE `t30`.`divid` = 'NULL' " ORDER BY `t10`.`wpname` ";
    $SQL_Where "WHERE `t30`.`divid` = 0 ORDER BY `t10`.`wpname` ";

    //Versuch2:
    $SQL_Where "WHERE `t30`.`divid` === NULL" " ORDER BY `t10`.`wpname`";

    //Versuch3:
    $SQL_Where "WHERE " . empty(`t30`.`divid`) . " ORDER BY `t10`.`wpname`";

    //Versuch4:
    $SQL_Where "WHERE " . (!isset(`t30`.`divid`)) . " ORDER BY `t10`.`wpname`";
    $SQL_Where "WHERE " . (NULL(`t30`.`divid`)) . " ORDER BY `t10`.`wpname`"
    Wenn ein Variable nicht gesetzt ist kann man dies mit isset prüfen. Ich weiß allerdings nicht wie ich das innerhalb einer
    Where-Klausel machen kann. Anhand meiner aufgelisteten Beispiele seht ihr das ich schon einiges ausprobiert habe...
    Kann mir hier jemand den richtigen Syntax verraten?



  • #2
    where ... IS NULL
    PostgreSQL Development, 24x7 Support, Remote DBA, Training & Services

    Kommentar


    • #3
      PHP-Code:
      $SQL_Where "WHERE " is null( . "`t30`.`divid`" . ) . " ORDER BY `t10`.`wpname` "
      Bekomme hier einen Syntax-Error: Parse error: syntax error, unexpected 'null' (T_STRING)

      Kommentar


      • #4
        Habe es noch mal so probiert, da kommt aber auch die Meldung: "Parse error: syntax error, unexpected 'null' (T_STRING)"
        PHP-Code:
        $SQL_Where "WHERE `t30`.`divid` "is null " ORDER BY `t10`.`wpname` "

        Kommentar


        • #5
          Fehler gefunden:

          PHP-Code:
          $SQL_Where "WHERE `t30`.`divid` IS NULL " " ORDER BY `t10`.`wpname` "

          Kommentar


          • #6
            Code:
            bdr3=# create table dih(id int, val text);
            NOTICE:  table dih without REPLICA IDENTITY was added to replication set bdr3 which replicates updates and deletes, UPDATE and DELETE on table will be disabled
            HINT:  To enable updating the table and deleting from the table, set REPLICA IDENTITY using ALTER TABLE
            CREATE TABLE
            bdr3=# insert into dih values (0, 'null asl int-wert');
            INSERT 0 1
            bdr3=# insert into dih values (null, 'null asl null');
            INSERT 0 1
            bdr3=# insert into dih (val) values ('id weggelassen');
            INSERT 0 1
            bdr3=# select * from dih ;
             id |        val        
            ----+-------------------
              0 | null asl int-wert
                | null asl null
                | id weggelassen
            (3 rows)
            
            bdr3=# select * from dih where id is null;
             id |      val       
            ----+----------------
                | null asl null
                | id weggelassen
            (2 rows)
            die NOTICE ignorieren, das kommt von BDR und ist hier irrelevant.
            PostgreSQL Development, 24x7 Support, Remote DBA, Training & Services

            Kommentar

            Lädt...
            X