Wieder mal SQL

Buba235

Erfahrenes Mitglied
Hallo!

Ich weiß dass ich hier in diesem Forum nicht richtig bin, aber ich weiß auch dass hier einige Leute sind, die mir schon oft und sehr gut geholfen haben. Aus diesem Grund stell ich die Frage hier. Also ich habe 2 SQL-Befehle und ich weiß, dass sie richtig sind. Nur arbeite ich auf MySQL und da haben wir schon das Problem. Also erst mal die beiden Befehle:

Code:
sql_Request = "UPDATE test SET ID_Link = ID \
                        WHERE ID IN (SELECT t1.ID \
                                FROM test t1, test t2 \
                        WHERE t1.PACKET_ID = t2.PACKET_ID \
                                AND t1.DST_IP = t2.SRC_IP \
                                AND t1.DATE = t2.DATE \
                                AND (t2.TIME - t1.TIME) <= 5 * 60 \
                                AND test.TYPE = 'Request')";
und

Code:
sql_Response = "UPDATE test tt SET ID_Link = (SELECT ID \
                                FROM test t \
                        WHERE t.PACKET_ID = tt.PACKET_ID \
                                AND t.DST_IP = tt.SRC_IP \
                                AND t.DATE = tt.DATE \
                                AND (tt.TIME - t.TIME) <= 5 * 60 \
                                AND t.TYPE = 'Request') \
                        WHERE ID IN (SELECT t1.ID \
                                FROM test t1, test t2 \
                        WHERE t1.PACKET_ID = t2.PACKET_ID \
                                AND t1.SRC_IP = t2.DST_IP \
                                AND t1.DATE = t2.DATE \
                                AND (t1.TIME - t2.TIME) <= 5 * 60 \
                                AND tt.TYPE = 'Response')";

Soweit also mal zu den Befehlen. So jetzt zu den Problem von MySQL. MySQL ist nicht fähig ein UPDATE und SELECT gleichzeitig zu machen:
Currently, you cannot update a table and select from the same table in a subquery.

Das ist mein Problem. Okay, es gibt ein Workaround:
http://www.xaprb.com/blog/2006/06/23/how-to-select-from-an-update-target-in-mysql/
Aber ich check nicht wie das gehen soll. Kann mir da jemand helfen? Wäre nett. Ich bin nämlich echt schon am Verzweifeln!

Gruß
Buba
 
Hallo!

Ich hab die Lösung gefunden. Für jeden, den es interessiert, hier die Lösung:

Code:
Update Test Set ID_Link = ID
	Where ID in (Select t1.ID
		From (Select * from Test) t1, (Select * from Test) t2
	Where t1.Packet_ID = t2.Packet_ID
		And t1.DST_IP = t2.SRC_IP
                        And t1.Date = t2.Date
                        And (t2.time - t1.time) <= 5 * 60
                        And trim(lower(t1.Type)) = 'request'
		AND t1.ID_LINK IS NULL);

Code:
Update Test Set ID_Link = (Select ID
		From (Select * from Test) as t
	Where t.Packet_ID = Test.Packet_ID
                        And t.DST_IP = Test.SRC_IP
                        And t.Date = Test.Date
                        And (Test.time - t.time) <= 5*60
                        And trim(lower(t.type)) = 'request')
	Where ID in (Select t1.ID
		From (Select * from Test) as t1, (Select * from Test) as t2
	Where t1.Packet_ID = t2.Packet_ID
		And t1.SRC_IP = t2.DST_IP
                      	And t1.Date = t2.Date
                      	And (t1.time - t2.time) <= 5*60
                      	And trim(lower(t1.Type)) = 'response'
		AND t1.ID_Link IS NULL);

Es musste über Umwege gemacht werden.
From (Select * from Test) as t1, (Select * from Test) as t2
Das musste eingefügt werden. Allerdings geht das nur ab einer Version, die höher als 4.1 ist!
 
Zurück