MySQL Forums
Forum List  »  German

Re: Anfängerfrage mehrere Bedingungen
Posted by: Thomas Wiedmann
Date: January 26, 2012 06:39PM

Hallo Tim,

mit benutzerdefinierten Variablen kann es zum Beispiel so funktionieren.

CREATE TABLE gesamtbetrag ( 
 id INT NOT NULL, 
 betrag DEC(6,2), 
 schalter1 VARCHAR(2), 
 schalter2 VARCHAR(2),
 schalter3 VARCHAR(2),
 PRIMARY KEY (id) 
); 

INSERT INTO gesamtbetrag VALUES 
( 1, 100, 'on', 'on', 'on' ), 
( 2, 200, ' ' , 'on', ' '); 


/**
* Zwischenergebnis in eine Uservariable speichern
* Benutzerdefinierte Variablen sind verbindungsspezifisch
*/
SET @betrag = (SELECT SUM(anzahl) FROM
               ( SELECT CASE 
                         WHEN schalter1 = 'on' THEN 11 
                         ELSE 0
                        END AS anzahl
                   FROM gesamtbetrag 
                  WHERE id = 1
                  
                  UNION ALL
                  
                 SELECT CASE 
                         WHEN schalter2 = 'on' THEN 22 
                         ELSE 0
                        END AS anzahl
                   FROM gesamtbetrag 
                  WHERE id = 1
                  
                  UNION ALL
                                               
                 SELECT CASE 
                         WHEN schalter3 = 'on' THEN 33 
                         ELSE 0
                        END AS anzahl
                   FROM gesamtbetrag 
                  WHERE id = 1
               ) summe_schalter
              );
              
              
mysql> select @betrag;
+---------+
| @betrag |
+---------+
|      66 |
+---------+
1 row in set (0.00 sec)

mysql>


UPDATE gesamtbetrag
   SET betrag = betrag + @betrag
 WHERE id = 1;
 
mysql> SELECT * FROM gesamtbetrag;
+----+--------+-----------+-----------+-----------+
| id | betrag | schalter1 | schalter2 | schalter3 |
+----+--------+-----------+-----------+-----------+
|  1 | 166.00 | on        | on        | on        |
|  2 | 200.00 |           | on        |           |
+----+--------+-----------+-----------+-----------+
2 rows in set (0.02 sec)

mysql>

Grüße
Thomas

Options: ReplyQuote


Subject
Views
Written By
Posted
2011
January 23, 2012 07:39AM
810
January 23, 2012 08:45AM
977
January 23, 2012 01:42PM
1009
January 24, 2012 05:54AM
796
January 25, 2012 01:32AM
811
January 25, 2012 06:40AM
867
January 26, 2012 10:06AM
Re: Anfängerfrage mehrere Bedingungen
953
January 26, 2012 06:39PM
862
January 29, 2012 06:35AM
964
January 23, 2012 08:30AM


Sorry, you can't reply to this topic. It has been closed.

Content reproduced on this site is the property of the respective copyright holders. It is not reviewed in advance by Oracle and does not necessarily represent the opinion of Oracle or any other party.