Delphi-PRAXiS
Seite 1 von 2  1 2      

Delphi-PRAXiS (https://www.delphipraxis.net/forum.php)
-   Datenbanken (https://www.delphipraxis.net/15-datenbanken/)
-   -   Delphi SQL-Problem mal wieder (https://www.delphipraxis.net/134262-sql-problem-mal-wieder.html)

Grolle 18. Mai 2009 19:54

Datenbank: Firebird • Version: 2.1 • Zugriff über: IBDAC

SQL-Problem mal wieder
 
Hallo,
ich habe mal wieder ein kleines Problem mit einer Abfrage:

SQL-Code:
SELECT tree.id, tree.name, tree2contact.contact_id FROM tree left join
tree2contact on tree.id = tree2contact.tree_id where tree2contact.contact_id=22
Die Tabellen sehen so aus:
SQL-Code:
Tree
---------
id
name
parent

tree2contact
---------
id
tree_id
contact_id
Ich will jetzt alle tree.name die tree.parent=0 ohne Dubletten(tree.name) und tree2contact.contact_id=22 (oder 0 wenn nicht zugeordnet).

Viele Grüße ...

hoika 18. Mai 2009 20:05

Re: SQL-Problem mal wieder
 
Hallo,

jaaaaa, wo ist denn das Problem ?

Zitat:

tree.name die tree.parent=0 ohne Dubletten(tree.name)
SQL-Code:
Select Distinct(Tree.Name), Tree.Id ...
Zitat:

und tree2contact.contact_id=22 (oder 0 wenn nicht zugeordnet).
SQL-Code:
Where ((tree2contact.contact_id=22) or (tree2contact.contact_id=0))
oder

SQL-Code:
Where ((tree2contact.contact_id=22) or (tree2contact.contact_id is Null))

Heiko

Grolle 18. Mai 2009 20:12

Re: SQL-Problem mal wieder
 
Zitat:

Zitat von hoika
jaaaaa, wo ist denn das Problem ?
SQL-Code:
Where ((tree2contact.contact_id=22) or (tree2contact.contact_id is Null))

:wall: Da war das Problem

hoika 18. Mai 2009 20:15

Re: SQL-Problem mal wieder
 
Hallo,

tree.parent=0

hatte ich noch vergessen.


Heiko

Grolle 18. Mai 2009 20:52

Re: SQL-Problem mal wieder
 
Hi,

so ganz klappt das immer noch nicht. Die Anfrage sieht jetzt so aus:
SQL-Code:
SELECT distinct(tree.name) as name, tree.id as id, tree2contact.contact_id as contact_id from tree left join tree2contact on tree.id=tree2contact.tree_id
Where ((tree2contact.contact_id=25) or (tree2contact.contact_id is Null)) and tree.parent=0 order by tree.name
Bei Abfragen mit unterschiedlicher (tree2contact.contact_id=25) bekomme tree.name verschiedener Anzahl? Eigentlich müsste er mir doch alle rausschmeißen, oder überseh ich da was?

Viele Grüße ...

hoika 18. Mai 2009 20:57

Re: SQL-Problem mal wieder
 
Hallo,

du willst also nur einen Datensatz haben, oder
was ist mit

Zitat:

bekomme tree.name verschiedener Anzahl
gemeint.

Mal bitte Bsp.-Daten und was rauskommen soll.


Heiko

Grolle 18. Mai 2009 21:15

Re: SQL-Problem mal wieder
 
SQL-Code:
Tree
(ID, NAME, PARENT)
(6, 'Kunden', 0);
(8, 'Behörden', 0);
(9, 'Banken', 0);
(10, 'Intern', 6);

Tree2Contact
(ID, TREE_ID, CONTACT_ID)
(1, 8, 22);
(2, 6, 25);
(3, 6, 22);
SQL-Code:
SELECT distinct(tree.name) as name, tree.id as id, tree2contact.contact_id as contact_id from tree left join tree2contact on tree.id=tree2contact.tree_id
Where ((tree2contact.contact_id=25) or (tree2contact.contact_id is Null)) and tree.parent=0 order by tree.name
soll:
('Banken', 9, NULL);
('Behörden', 8, NULL);
('Kunden', 6, 25);
ist:
('Banken', 9, NULL);
('Kunden', 6, 25);


bei:
SQL-Code:
SELECT distinct(tree.name) as name, tree.id as id, tree2contact.contact_id as contact_id from tree left join tree2contact on tree.id=tree2contact.tree_id
Where ((tree2contact.contact_id=22) or (tree2contact.contact_id is Null)) and tree.parent=0 order by tree.name
soll:
('Banken', 9, NULL);
('Behörden', 8, 22);
('Kunden', 6, 22);
und ist auch so. (Teste mit IBExpert)

viele Grüße ...

hoika 19. Mai 2009 07:33

Re: SQL-Problem mal wieder
 
Hallo,

melkwüldig ;)

Hänge mal die FDB an den 1. Thread an,
oder poste sie mal mit Hinweis auf den vorigen.


Heiko

alex517 19. Mai 2009 08:02

Re: SQL-Problem mal wieder
 
SQL-Code:
SELECT
  distinct(tree.name) as name,
  tree.id as id,
  tree2contact.contact_id as contact_id
from
  tree
  left join tree2contact on tree.id=tree2contact.tree_id and
                            ((tree2contact.contact_id=25) or (tree2contact.contact_id is Null))
Where
  tree.parent=0
order by
  tree.name
alex

hoika 19. Mai 2009 12:23

Re: SQL-Problem mal wieder
 
Hallo alex517,

wo ist da der Unterschied ? (interessiert mich wirklich)
der Left Join bezieht sich ja nur auf das Tree(Id)


Heiko


Alle Zeitangaben in WEZ +1. Es ist jetzt 04:01 Uhr.
Seite 1 von 2  1 2      

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO © 2011, Crawlability, Inc.
Delphi-PRAXiS (c) 2002 - 2023 by Daniel R. Wolf, 2024 by Thomas Breitkreuz