WCM - Das österreichische Computer Magazin Forenübersicht
 

Zurück   WCM Forum > Rat & Tat > Programmierung

Programmierung Rat & Tat für Programmierer

Microsoft KARRIERECAMPUS

Antwort
 
Themen-Optionen Ansicht
Alt 19.12.2006, 17:43   #1
jak
Inventar
 
Registriert seit: 13.06.2001
Beiträge: 1.830


Standard 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
1   | a        | 0
2   | b        | 0
3   |          | 2
4   | c        | 0
Das gesuchte SQL statement sollte als wenn category = b als Ergebnis den 2. und 3. Eintrag liefern.

Danke für Vorschläge
jak
____________________________________
Join the DNRC | Godwin\'s Law (thx@stona)
Documentation is like sex: If it\'s good, it\'s very, very good. If it\'s bad, it\'s better than nothing.
\"In theory, theory and practice are the same. In practice, they are not\" (Lawrence Berra)
jak ist offline   Mit Zitat antworten
Alt 22.12.2006, 13:24   #2
jak
Inventar
 
Registriert seit: 13.06.2001
Beiträge: 1.830


Standard

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:
Posted by Kim Mihaly on October 4 2006 5:35am

For an app I was writing I needed to list both parent & child for a 2 level tree - the first half of the union returns all the parents , the second all the children for these parents - as I was using the pear html_pager I also added a limit clause....

select parent.categoryid, parent.categoryname, parent.sequence as seq, parent.categoryid as id, '' as name, 0 as seq1
from category as parent
where parent.isasubcategoryof = 0
union
select parent.categoryid, parent.categoryname, parent.sequence as seq, child.categoryid as id, child.categoryname as name, child.sequence as seq1
from category as parent, category as child
where (child.isasubcategoryof = parent.categoryid)
order by seq, seq1
jak
____________________________________
Join the DNRC | Godwin\'s Law (thx@stona)
Documentation is like sex: If it\'s good, it\'s very, very good. If it\'s bad, it\'s better than nothing.
\"In theory, theory and practice are the same. In practice, they are not\" (Lawrence Berra)
jak ist offline   Mit Zitat antworten
Alt 22.12.2006, 19:16   #3
iG0r
Gesperrt
 
Registriert seit: 14.08.2003
Alter: 47
Beiträge: 915


Standard

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?
iG0r ist offline   Mit Zitat antworten
Alt 23.12.2006, 00:15   #4
jak
Inventar
 
Registriert seit: 13.06.2001
Beiträge: 1.830


Standard

Tabelle: tab
Code:
UID | category | title | other | copyof
1   | a        | asd   | foo      0
2   | b        | fgh   | bar      0
3   |          | jkl   |          2
4   | c        | yxc   | qwe      0
5   | c        | vbn   | rtz      2
Eintrag 3 hat copyof = 2, d.h. er ist eine Kopie des zweiten Eintrags. Wenn er in einem Feld einen Eintrag hat (z.B. title), gilt dieser, sonst gilt der Eintrag der Zeile deren ID = 2 ist.

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
1   | 0        | a        | NULL          | asd   | NULL       | foo   | NULL
2   | 0        | b        | NULL          | fgh   | NULL       | bar   | NULL
4   | 0        | c        | NULL          | yxc   | NULL       | qwe   | NULL
3   | 2        |          | b             | jkl   | fgh        |       | bar 
5   | 2        | c        | b             | jkl   | fgh        | rtz   | bar
Nachteil: die Statements werden ziemlich unübersichtlich. Wenn ich z.B nur Einträge mit Kategorie b haben will, sieht das so aus:
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
2   | 0        | b        | NULL          | fgh   | NULL       | bar   | NULL
3   | 2        |          | b             | jkl   | fgh        |       | bar
Wenn es also eine bessere Methode gibt freue ich mich, ansonsten kann ich auch mit der aktuellen Methode leben.

jak
____________________________________
Join the DNRC | Godwin\'s Law (thx@stona)
Documentation is like sex: If it\'s good, it\'s very, very good. If it\'s bad, it\'s better than nothing.
\"In theory, theory and practice are the same. In practice, they are not\" (Lawrence Berra)
jak ist offline   Mit Zitat antworten
Antwort


Aktive Benutzer in diesem Thema: 1 (Registrierte Benutzer: 0, Gäste: 1)
 

Forumregeln
Es ist Ihnen nicht erlaubt, neue Themen zu verfassen.
Es ist Ihnen nicht erlaubt, auf Beiträge zu antworten.
Es ist Ihnen nicht erlaubt, Anhänge hochzuladen.
Es ist Ihnen nicht erlaubt, Ihre Beiträge zu bearbeiten.

BB-Code ist an.
Smileys sind an.
[IMG] Code ist an.
HTML-Code ist aus.

Gehe zu


Alle Zeitangaben in WEZ +2. Es ist jetzt 17:51 Uhr.


Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Forum SEO by Zoints
© 2009 FSL Verlag