query ilişkili tabloda 2 kez listeliyor

Delphi'de kod yazma ile ilgili sorularınızı bu foruma yazabilirsiniz.
Cevapla
serkan
Üye
Mesajlar: 666
Kayıt: 10 Tem 2003 12:08
Konum: bursa

query ilişkili tabloda 2 kez listeliyor

Mesaj gönderen serkan »

selam arkadaşlar,dün ilk kez ilişkili tablolarda query kullanayım dedim,
demez olsaydım.stok kartı ve stok hareketlerinin tutulduğu iki tablom var bunları;
select * from stok,stkhar where stok.stokkodu=stkhar.stokkodu
şeklinde ilişkilendirdim.
dbgrid de stok kartına ait bilgileri listelediğimde tüm kayıtlar iki kez altalta listeleniyor.ilkkez denediğim için ya biryerde yanlış yapıyorum ya da sql denen meret böyle yapıyor..
çözümü nedir bunun arkadaşlar..[/code]
Kullanıcı avatarı
lazio
Moderator
Mesajlar: 1527
Kayıt: 11 Tem 2003 04:55
Konum: İstanbul

Mesaj gönderen lazio »

Kod: Tümünü seç

select * from stok LEFT JOIN stkhar ON (stok.stokkodu=stkhar.stokkodu)
Kullanıcı avatarı
Kuri_YJ
Moderator
Mesajlar: 2248
Kayıt: 06 Ağu 2003 12:07
Konum: İstanbul
İletişim:

Mesaj gönderen Kuri_YJ »

Merhaba,

Öncelikle bir uyarı, bu tür SQL sorularınızı Veritabanı ve SQL isimli bölümde sorun.

Yanıta gelince,

Kod: Tümünü seç

TABLO1
STKKOD  STOKAD
0001       KALEM
0002       DEFTER
0003       SILGI

TABLO2
HRSTKOD  HRKTIP  TARIH         MIKTAR
0001         GIRIS     01.01.2003 10 ADET
0003         GIRIS     10.01.2003 10 ADET
0001         GIRIS     10.01.2003   5 ADET
0001         GIRIS     15.01.2003   5 ADET

Eğer SQL aşağıdaki gibi olursa

Kod: Tümünü seç

  SELECT * FROM TABLO1, TABLO2 WHERE TABLO1.STKKOD = TABLO2.HRSTKOD
dediğinde kayıtları birbirine eşleştirip getirir. Yani,

Kod: Tümünü seç

0001       KALEM  0001         GIRIS     01.01.2003 10 ADET
0003       SILGI   0003         GIRIS     10.01.2003 10 ADET
0001       KALEM  0001         GIRIS     10.01.2003   5 ADET
0001       KALEM  0001         GIRIS     10.01.2003   5 ADET
0001       KALEM  0001         GIRIS     15.01.2003   5 ADET
eşleşen kayıtları getirir, eşleşmeyeni getirmez. Bunları Unique'leştirmek istiyorsan, OUTER, INNER gibi Join kombinasyonlarını incele.

Kolay gelsin.
Kuri Yalnız Jedi
Harbi Özgürlük İçin Pisi http://www.pisilinux.org/
serkan
Üye
Mesajlar: 666
Kayıt: 10 Tem 2003 12:08
Konum: bursa

Mesaj gönderen serkan »

cevaplarınızı denemeye vaktim olmadı..lazio yazdığın kod un anlamı ne yani bu komut ne işe yarıyor...
Kullanıcı avatarı
rsimsek
Admin
Mesajlar: 4482
Kayıt: 10 Haz 2003 01:48
Konum: İstanbul

Mesaj gönderen rsimsek »

her iki select de de join işlemi yapılmıştır. çeşitli join yapıları vardır. ikinci tabloda olanlar, olmıyanlar veya her ikisi de diyebilirsin. helpinden kullanım formatına bakabilirsin.
burada tekrarlayan kayıtlar gelmesin diyorsan

Kod: Tümünü seç

 select distinct ... 
şeklinde kullanmalısın.

genel yapı aşağıdaki gibidir.

localsql.hlp dosyasından alıntı;
Joins two tables based on column values common between the two, including non-matches.

SELECT column_list

FROM table_reference
LEFT | RIGHT | FULL [OUTER] JOIN table_reference
ON predicate
[LEFT | RIGHT | FULL [OUTER] JOIN table_reference

ON predicate...]

Description

Use an OUTER JOIN to join two tables, a source and joining table, that have one or more columns in common. One or more columns from each table are compared in the ON clause for equal values. The primary difference between inner and outer joins is that, in outer joins rows from the source table that do not have a match in the joining table are not excluded from the result set. Columns from the joining table for rows in the source table without matches have NULL values.

In the statement below, the CUSTOMER and ORDERS tables are joined based on values in the CUSTNO column, which each table contains. For rows from CUSTOMER that do not have a matching value between CUSTOMER.CUSTNO and ORDERS.CUSTNO, the columns from ORDERS contain NULL values.

SELECT *

FROM Customer C
LEFT OUTER JOIN Orders O

ON (C.CustNo = O.CustNo)

The LEFT modifier causes all rows from the table on the left of the OUTER JOIN operator to be included in the result set, with or without matches in the table to the right. If there is no matching row from the table on the right, its columns contain NULL values. The RIGHT modifier causes all rows from the table on the right of the OUTER JOIN operator to be included in the result set, with or without matches. If there is no matching row from the table on the left, its columns contain NULL values. The FULL modifier causes all rows from the all tables specified in the FROM clause to be included in the result set, with or without matches. If there is no matching row from one of the tables, its columns contain NULL values.

More than one table may be joined with an INNER JOIN. One use of the INNER JOIN operator and corresponding ON clause is required for each each set of two tables joined. One column comparison predicate in an ON clause is required for each column compared to join each two tables. The statement below joins the CUSTOMER table to ORDERS, and then ORDERS to ITEMS. In this case, the joining table ORDERS acts as a source table for the joining table ITEMS.

SELECT *

FROM Customer C
FULL OUTER JOIN Orders O
ON (C.CustNo = O.CustNo)
FULL OUTER JOIN items I

ON (O.OrderNo = I.OrderNo)

Tables may also be joined using expressions to produce a single value for the join comparison predicate. Here, the ID1 and ID2 columns in JOINING are separately compared with two values produced by the SUBSTRING function using the single column ID in SOURCE.

SELECT *

FROM Source S
RIGHT OUTER JOIN Joining J
ON (SUBSTRING(S.ID FROM 1 FOR 2) = J.ID1) AND

(SUBSTRING(S.ID FROM 3 FOR 1) = J.ID2)

An ORDER BY clause in OUTER JOIN statements can use columns from any table specified in the FROM clause to sort the result set.
Cevapla