|
Yazan
|
Konu: 3 Ayrı tablodan veri çekmek ? (Okunma Sayısı 1730 defa)
|
Witkey
Ziyaretçi
|
3 ayrı tablodan çeşitli veriler çekmek istiyorum ALT sorgularla degil bunu başka nasıl yapabilirim ?
İyi çalışmalar, Onur Yerlikaya
|
|
|
|
|
Logged
|
|
|
|
shapcy
Mustafa Şapçılı
Admin
Offline
Mesaj Sayısı: 811
PHP Coding...
|
Hmm, biraz daha açık anlatabilir misin Onur? Yani bunu herşekilde yaparsın. SELECT * FROM t1,t2,t3 işine yaramayacak sanırım ama bu sorunun cevabı 
|
|
|
|
|
Logged
|
|
|
|
Witkey
Ziyaretçi
|
3 adet tablo var Mustafa bu tablolar resimler , kategori , isimler gibi düşün. bu 3 tablo birbirleriyle alakalı isme ait 2 tane resim olsa bile tekini almak istiyorum ama altsorgularla degil  anlamadıysan boşver , VIEW oluşturmayı düşünüyorum. İyi Çalışmalar, Onur Yerlikaya
|
|
|
|
|
Logged
|
|
|
|
Witkey
Ziyaretçi
|
Neyse view'e de gerek kalmadı alt sorgular ve AS ile hallettim
|
|
|
|
|
Logged
|
|
|
|
|
aziz
|
SELECT * FROM t1 LEFT JOIN (t2, t3, t4) ON (t2.a=t1.a AND t3.b=t1.b AND t4.c=t1.c) #yada SELECT * FROM table1,table2 WHERE table1.id=table2.id;
...gibi sonsuz tablo seçeneğinden veri çekebilirsin witkey. Burada tek önemli olan indexler ve primary_key ler. Bunların her tabloda , tabloların etkileşmelerini sağlayacak eşit değerleri olması lazım. Bu sayede 3 değil 1033 tablo bile bağlanır İyi Çalışmalar
|
|
|
|
|
Logged
|
|
|
|
Witkey
Ziyaretçi
|
SELECT DISTINCT products.pid, products.pname, products.pcolor, products.pserial, category.cname, ( SELECT images.thumb FROM images WHERE images.pid = products.pid LIMIT 0,1 ) AS thumb FROM products, category, images WHERE products.pid = images.pid GROUP BY images.id DESC ORDER BY products.pdate DESC LIMIT 0,3
Altsorgularla yapmayacaktım Sonra "AS" deyimini ogrendim ve dizi icerisinde gonderirken bu problemi de giderdim. Diğer taraftan aziz senin mesajına gelince LEFT JOIN'i felan anlamış degilim.Altta verdiğin örnekteki gibi yapmıştım zaten ama işte birbirleriyle ilişkili derken ürünün resmi resimlerde categorisi kategorilerde birden fazla resim var ama 1 tane almalı gibi karmaşık birşey  Teşekkürler, İyi çalışmalar, Onur Yerlikaya
|
|
|
|
|
Logged
|
|
|
|
|
aziz
|
Aslında aynı PHP gibi. Bir tek seçeneğin yok, çeşitli yollarla aynı yere çıkılabiliyor. Zaten programlamanın güzel yeri bu.
|
|
|
|
|
Logged
|
|
|
|
|
muratmoon
|
Ürünün tek bir kategorisi olduğunu düşünürsek resimlerin birden fazla olması problem değil, Grupladığında zaten tek bir tane gelecektir.
Ayrıca resim tablosunu bağladığın halde tekrar alt sorguda kullanmışsın, kategoriler tablosunu ise hiç bağlamamışsın bile.
Özetle senin sorgunda hata var, aziz'in örneğindeki şekilde kullanman daha uygun.
|
|
|
|
|
Logged
|
|
|
|
Witkey
Ziyaretçi
|
SELECT DISTINCT products.pid, products.pname, products.pcolor, products.pserial, category.cname, category.cid, ( SELECT images.thumb FROM images WHERE images.pid = products.pid LIMIT 0,1 ) AS thumb FROM products, category, images WHERE products.pid = images.pid AND products.pCat = category.cid GROUP BY images.id DESC ORDER BY products.pdate DESC
Evet hata olduğunu o gün düzelttim fakat buraya yazmak istemedim Murat hocam, Diğer taraftan Grublamam dahilinde olmadıgı ıcın bu yolda yapmak zorunda kaldım... Şu an gayet güzel calısıyor ama eksigim varsa bilmek isterim 
|
|
|
|
|
Logged
|
|
|
|
|
enginna
|
alt sorgu yerine tablo birleştirme kullanılsa daha iyi olmaz mı?
|
|
|
|
|
Logged
|
bir fikrim var. internetteki siteleri kaydeden bir site yapalım. isteyen bu siteler içinde arama yapsın. sitenin domaini'de google.com olsun nasıl? 
|
|
|
|
oktay
|
Ne yapmak istediğini anlamadım ama birincisi eğer ki bu şekilde DISTINCT ile bir resim alman gerekiyosa tablo dizaynında bir gariplik var derim. İkincisi de LEFT JOIN ile halledilebilir gibi görünüyor subquery kullanmak yerine.
oktay
|
|
|
|
|
Logged
|
|
|
|
Witkey
Ziyaretçi
|
Diğer taraftan aziz senin mesajına gelince LEFT JOIN'i felan anlamış degilim.
DISTINCT'in orada bir degeri yok acıkcası diger taraftan tabloları birlestirme gibi bir secenek yok enginna  VIEW oluşturacaktım fakat hallettim şu an fakat daha kısa bir yolu ve yahut yanlışım varsa öğrenmek isterim. LEFT JOİNİ ufak bir örnek ile anlatacak arkadaş olursa da sevinirim. İyi çalışmalar, Onur Yerlikaya
|
|
|
|
|
Logged
|
|
|
|
|
enginna
|
tabloları birlestirme gibi bir secenek yok enginna  üç tablodan veri çekip bunu sayfada yansıtmak istiyorsun tablo birleştirme kullanabilirsin. zaten left join'de bir tablo birleştirme yönetimidir. (ayrıca benim bildiğim kadarıyla tam emin değilim ama tablo birleştirme alt sorgulardan daha çok tavsiye ediliyor.)
|
|
|
|
|
Logged
|
bir fikrim var. internetteki siteleri kaydeden bir site yapalım. isteyen bu siteler içinde arama yapsın. sitenin domaini'de google.com olsun nasıl? 
|
|
|
|
aziz
|
SELECT * FROM t1 LEFT JOIN (t2, t3, t4) ON (t2.a=t1.a AND t3.b=t1.b AND t4.c=t1.c) Bu örnekten yola çıkarsak, t1 tablosu t2,t3 ve t4 tablolarına soldan bağlanıyor t2 tablosunun a sütununun t1 tablosunun a sütununa eşit olduğu yerde "ve" t3 tablosunun b sütununun t1 tablosunun b sütununa eşit olduğu yerde. Soldan bağlanıyor ne demek, MySQL implements an A LEFT JOIN B join_condition as follows: • Table B is set to depend on table A and all tables on which A depends. • Table A is set to depend on all tables (except B) that are used in the LEFT JOIN condition. • The LEFT JOIN condition is used to decide how to retrieve rows from table B. (In other words, any condition in the WHERE clause is not used.) • All standard join optimizations are performed, with the exception that a table is always read after all tables on which it depends. If there is a circular dependence, MySQL issues an error. • All standard WHERE optimizations are performed. • If there is a row in A that matches the WHERE clause, but there is no row in B that matches the ON condition, an extra B row is generated with all columns set to NULL. • If you use LEFT JOIN to find rows that do not exist in some table and you have the following test: col_name IS NULL in the WHERE part, where col_name is a column that is declared as NOT NULL, MySQL stops searching for more rows (for a particular key combination) after it has found one row that matches the LEFT JOIN condition. The implementation of RIGHT JOIN is analogous to that of LEFT JOIN with the roles of the tables reversed. The join optimizer calculates the order in which tables should be joined. The table read order forced by LEFT JOIN or STRAIGHT_JOIN helps the join optimizer do its work much more quickly, because there are fewer table permutations to check. Note that this means that if you do a query of the following type, MySQL does a full scan on b because the LEFT JOIN forces it to be read before d: SELECT * FROM a,b LEFT JOIN c ON (c.key=a.key) LEFT JOIN d ON (d.key=a.key) WHERE b.key=d.key; The fix in this case is reverse the order in which a and b are listed in the FROM clause: SELECT * FROM b,a LEFT JOIN c ON (c.key=a.key) LEFT JOIN d ON (d.key=a.key) WHERE b.key=d.key; For a LEFT JOIN, if the WHERE condition is always false for the generated NULL row, the LEFT JOIN is changed to a normal join. For example, the WHERE clause would be false in the following query if t2.column1 were NULL: SELECT * FROM t1 LEFT JOIN t2 ON (column1) WHERE t2.column2=5; Therefore, it is safe to convert the query to a normal join: SELECT * FROM t1, t2 WHERE t2.column2=5 AND t1.column1=t2.column1; This can be made faster because MySQL can use table t2 before table t1 if doing so would result in a better query plan. To force a specific table order, use STRAIGHT_JOIN. Çeviremedim, kusura bakmayın
|
|
|
|
|
Logged
|
|
|
|
Witkey
Ziyaretçi
|
SELECT category.cname, category.cid, products.pid, products.pCat, products.pname, products.pcolor, products.pserial, images.thumb FROM products LEFT JOIN category ON products.pCat = category.cid LEFT JOIN images ON images.id = products.pid ORDER BY products.pdate DESC
şeklinde hallettim  , yardımcı olan herkese teşekkür ederim. İyi çalışmalar, Onur Yerlikaya
|
|
|
|
|
Logged
|
|
|
|
|
 |