MySQL Forums
Forum List  »  German

Problem mit SQL-Abfrage über mehrere Tabellen
Posted by: ralf lehmann
Date: November 27, 2006 04:29PM

Hi,
vielleicht kann mir einer der SQL-Cracks hier ein wenig unter die Arme greifen.
Verwende mysql 5.1.7-beta

Wie kann ich folgende 2 Abfragen in ein Statement packen?
Diese Abbfragen habe ich nur exemplarisch gewählt. Sie lassen sich durchaus auch so verwenden.
Allerdings taucht das gleiche Problem auch bei komplexeren Abfragen auf, die sich nicht ohne grössere Klimmzüge bei der anschließenden Verarbeitung des Resultset in 2 Abfragen splitten lassen.



String sqlReduziert="select Sum(Verkauf.Menge * (Verkauf.VK_preis/(Artikel.MwSt_reduziert+100)*100))"+
"from Verkauf,Artikel,Transaction"+
" where Verkauf.ArtNr=Artikel.ArtNr" +
" and Verkauf.IsBezahlt=?" +
" and Verkauf.TransactionNr=Transaction.LfNr" +
" and Transaction.ausserHaus='1'" +
" and Verkauf.Date between ? and ? ";

String sqlNormal="select Sum(Verkauf.Menge * (Verkauf.VK_preis/(Artikel.MwSt_normal+100)*100))"+
"from Verkauf,Artikel,Transaction"+
" where Verkauf.ArtNr=Artikel.ArtNr" +
" and Verkauf.IsBezahlt=?" +
" and Verkauf.TransactionNr=Transaction.LfNr" +
" and Transaction.ausserHaus='0'" +
" and Verkauf.Date between ? and ? ";

Tabellen definiton ist folgende:
Artikel
+----------------+--------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+----------------+--------------+------+-----+---------+-------+
| ArtNr | char(4) | NO | PRI | | |
| Bezeichnung | char(30) | NO | UNI | | |
| EK_Preis | decimal(6,2) | YES | | NULL | |
| VK_Preis | decimal(6,2) | NO | | 0.00 | |
| WarengruppenNr | char(3) | NO | MUL | | |
| MwSt_normal | decimal(2,0) | NO | | 16 | |
| MwSt_reduziert | decimal(2,0) | NO | | 7 | |
+----------------+--------------+------+-----+---------+-------+

Verkauf
+---------------+--------------+------+-----+-------------------+----------------+
| Field | Type | Null | Key | Default | Extra |
+---------------+--------------+------+-----+-------------------+----------------+
| LfNr | int(7) | NO | PRI | NULL | auto_increment |
| TransactionNr | int(7) | NO | MUL | | |
| ArtNr | char(4) | NO | MUL | | |
| Menge | decimal(6,2) | NO | | 0.00 | |
| Date | timestamp | YES | | CURRENT_TIMESTAMP | |
| VK_Preis | decimal(6,2) | NO | | 0.00 | |
| PosPreis | decimal(6,2) | YES | | NULL | |
| IsBezahlt | tinyint(1) | YES | | NULL | |
| Benutzer | char(20) | NO | MUL | | |
+---------------+--------------+------+-----+-------------------+----------------+

Transaction
+------------+------------+------+-----+-------------------+----------------+
| Field | Type | Null | Key | Default | Extra |
+------------+------------+------+-----+-------------------+----------------+
| LfNr | int(7) | NO | PRI | NULL | auto_increment |
| Date | timestamp | YES | | CURRENT_TIMESTAMP | |
| Benutzer | char(20) | NO | MUL | | |
| Open | tinyint(1) | NO | | | |
| ausserHaus | tinyint(1) | NO | | 1 | |
+------------+------------+------+-----+-------------------+----------------+

Options: ReplyQuote


Subject
Views
Written By
Posted
Problem mit SQL-Abfrage über mehrere Tabellen
4346
November 27, 2006 04:29PM


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.