[MySQL 4.1] @var := 'hallo'

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

  • [MySQL 4.1] @var := 'hallo'

    Hoi,

    hab mir mal, nach dem Tipp von Sebastian W, das Scripting von SQL angeschaut und bin gerade einen Test am machen.
    Leider hab ich ein Problem. Meine Ausgabe (name_short) ist immer leer unter MySQL 4.1.9

    Vielleicht kann mir einer von euch ja sagen wodrans liegt.
    Hier mein SQL
    Code:
    DECLARE @inhalt;
    SET @inhalt:='';
    SELECT @inhalt='hallo';
    SELECT @inhalt AS name_short;
    thnx

  • #2
    Da fehlt noch das ":" in der SELECT anweisung sonst weis MySQL nicht wos hingeht und denkt das ist eine auszuwertendender Ausdruck :

    (Ist variable gleicht 'hallo' ? )
    Zuletzt geändert von chansel0049; 08.06.2005, 17:04.
    chansel0049
    ----------------------------------------------------
    if you've reached the bottomline - dig further!
    Übersetzer gesucht? http://www.babelport.com

    Kommentar


    • #3
      Hmm, in welcher Select anweisung? Da wo die Zuweisung stattfindet, oder die mit der Ausgabe?

      Kommentar


      • #4
        Wo die Zuweisung erfolgt , sonst interpretiert MySQL das als einen auszuwertendender Ausdruck :

        (Ist variable gleicht 'hallo' ? )
        chansel0049
        ----------------------------------------------------
        if you've reached the bottomline - dig further!
        Übersetzer gesucht? http://www.babelport.com

        Kommentar


        • #5
          Also so

          Code:
          SET @inhalt:='';
          SELECT @inhalt:='hallo';
          SELECT @inhalt AS name_short;
          Dann ist 'name_short' leider immernoch leer.

          das Declare kann man getrost vergessen, ist wohl von Oracle. Ein Set reicht.

          Kommentar


          • #6
            hm ... wie wär's mit:

            set @a = 'hallo'
            select @a strhallo

            output: hallo
            EDIT:
            manchmal verstehe ich nicht, wie man einfach drauf los probieren kann, ohne die Doku mal gelesen zu haben. Was glaubt ihr, warum hat man 'ne Doku geschrieben? Um das Produkt zu verschönern oder was?

            Kommentar


            • #7
              Der Code ist korrekt,

              habs grad in PhpmyAdmin kopiert und getestet und auch mal über Konsole

              Wahrscheinlich liegt der Fehler im PHP
              chansel0049
              ----------------------------------------------------
              if you've reached the bottomline - dig further!
              Übersetzer gesucht? http://www.babelport.com

              Kommentar


              • #8
                hmm, asp - ist bei mir auch leer, einfach deins copy&pasted, noch die ; an jede Zeile und strhallo ist leer.

                Kommentar


                • #9
                  Wahr scheinlich soll der Variable erst mit der ersten SELECT-Anweisung ein Wert zugewiesen werden, zB in einer größeren Query
                  chansel0049
                  ----------------------------------------------------
                  if you've reached the bottomline - dig further!
                  Übersetzer gesucht? http://www.babelport.com

                  Kommentar


                  • #10
                    ich teste es mit mysqlfront, also ohne php....

                    checks grad mal in phpmyadmin...

                    na toll, da funktionierts - dann liegts wohl an meine dummen frontend.
                    kk, dann werd ich mich mal an die query geben...

                    dank euch allen...

                    Kommentar


                    • #11
                      Original geschrieben von prego
                      hmm, asp - ist bei mir auch leer, einfach deins copy&pasted, noch die ; an jede Zeile und strhallo ist leer.
                      dann schau mein Anhang an
                      Angehängte Dateien

                      Kommentar


                      • #12
                        asp, hab da nur en paar hints zusammen getragen - hab kein doku zu @variablen bei mysql gefunden. erleuchte mich wenn du nen link hast. Ausserdem kann ich ja viel probieren wenn mein front nichts ausspuckt...

                        Kommentar


                        • #13
                          So,

                          nachdem ich nun die QUERY in phpmyadmin bzw. NaviCat mache seh ich auch ne Ausgabe.

                          Hab meine Query zusammengebaut - funzt. Jetzt gibt es nur noch ein Problem.

                          Ich mache die Zuweisung der Variabel in nem Subselect. In etwa so
                          Code:
                          SET @inhalt = '';
                          SELECT 
                          name_short,
                          name_long,
                          
                          (
                          SELECT 
                          @inhalt:=CONCAT(@inhalt, wertx, ', ')) 
                          FROM tab2 
                          WHERE x=y
                          ) AS inhalt
                          
                          FROM
                          tab1
                          
                          WHERE
                          x=y;
                          Ist natürlich logisch, das @inhalt immer länger wird. Wie kann ich @inhalt für jede Row im Result leeren?

                          Achso, noch was... muss ich die @variablen wieder unsetten? Wenn ja, wie?

                          Any hints?

                          Kommentar


                          • #14
                            Ich muss zu oben noch was ergänzen.

                            In Wirklichkeit sieht die Query so aus....
                            Code:
                            SET @my_id='';
                            SET @inhalt = '';
                            SELECT 
                            name_short,
                            name_long,
                            @my_id := id,
                            
                            (
                            SELECT 
                            @inhalt:=CONCAT(@inhalt, wertx, ', ')) 
                            FROM tab2 
                            WHERE tab1_id = @my_id
                            ) AS inhalt
                            
                            FROM
                            tab1
                            
                            WHERE
                            x=y;
                            Wobei due Query dann saulangsam wird. 1,5sec gegenüber 0,2sec bei nichtverwendung einer Variable zur Abfrage im Subselect.

                            Kommentar


                            • #15
                              Another issue with setting a variable and using it in the same statement is that the default result type of a variable is based on the type of the variable at the start of the statement. The following example illustrates this:

                              mysql> SET @a='test';
                              mysql> SELECT @a,(@a:=20) FROM tbl_name;

                              For this SELECT statement, MySQL reports to the client that column one is a string and converts all accesses of @a to strings, even though @a is set to a number for the second row. After the SELECT statement executes, @a is regarded as a number for the next statement.

                              To avoid problems with this behavior, either do not set and use the same variable within a single statement, or else set the variable to 0, 0.0, or '' to define its type before you use it.

                              An unassigned variable has a value of NULL with a type of string.
                              http://dev.mysql.com/doc/mysql/en/variables.html
                              chansel0049
                              ----------------------------------------------------
                              if you've reached the bottomline - dig further!
                              Übersetzer gesucht? http://www.babelport.com

                              Kommentar

                              Lädt...
                              X