![]() |
mySQL 2 mal auf selbe Tabelle zugreifen
Folgende Situation:
Tabelle: date date.uid -> ID date.other -> restliche Daten date.category -> Kategorie date.copyof -> ID eines anderen date. Der Sinn von date.copyof wäre es, Kopien von dates zu erstellen, bei denen z.B. other und category leer sind. Jetzt würde ich gerne alle Dates aus einer bestimmten Kategorie suchen, d.h. ich müsste, wenn category leer ist die category aus dem Eintrag mit der uid = copyof verwenden. Beispiel: Code:
UID | category | copyof Danke für Vorschläge jak |
Die Lösung die ich verwendet habe sind zwei SELECTs die mit UNION zusammen gefasst werden. Das ist allerdings eher mühsam, da die WHERE statements für beide SELECTs angepasst werden müssen.
Von http://dev.mysql.com/doc/refman/4.1/en/union.html Zitat:
|
würde dir gerne helfen, verstehe aber deine frage leider nicht. wieso zweimal auf die tabelle zugreifen, und was hat es mit der kopie auf sich?
ich kann es nicht richtig verstanden haben, da bei mit nur ein select mit zwei where feld = 0 raus kommt. soll die kopie ein insert oder uptade query sein? |
Tabelle: tab
Code:
UID | category | title | other | copyof Meine Lösung ist jetzt in der Form: SELECT uid, 0 as orig_id, category, NULL as orig_category, title, NULL as orig_title, other, NULL as orig_other FROM tab WHERE copyof = 0 UNION SELECT copy.uid, orig.uid as orig_id, copy.category, orig.category as orig_category, copy.title, orig.title as orig_title, copy.other, orig.other as orig_other FROM tab AS orig, tab AS copy WHERE copy.copyof = orig.uid Das liefert: Code:
uid | orig_uid | category | orig_category | title | orig_title | other | orig_other SELECT uid, 0 as orig_id, category, NULL as orig_category, title, NULL as orig_title, other, NULL as orig_other FROM tab WHERE copyof = 0 AND category = 'b' UNION SELECT copy.uid, orig.uid as orig_id, copy.category, orig.category as orig_category, copy.title, orig.title as orig_title, copy.other, orig.other as orig_other FROM tab AS orig, tab AS copy WHERE copy.copyof = orig.uid WHERE ((copy.category = '' AND orig.category = b) OR (copy.category = 'b')) und liefert: Code:
uid | orig_uid | category | orig_category | title | orig_title | other | orig_other jak |
Alle Zeitangaben in WEZ +2. Es ist jetzt 15:53 Uhr. |
Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
© 2009 FSL Verlag