mysql bir sorgu ile iki farklı tablodan iki kaydı seçmek için nasıl

2 Cevap php

mysql bir sorgu ile iki farklı tablodan iki kayıt seçmek nasıl?

kullanarak bir arama sorgusu im

$search = $_GET['gd']
$arama_sonuc = mysql_query("select * from mp3 where baslik like '%$search%'");

well its ok. it shows me results from mp3 table. but with same query i need to search something on HABERLER table too.. how can i do? thanks...

2 Cevap

Use the UNION command http://dev.mysql.com/doc/refman/5.0/en/union.html

select * from mp3 where baslik like '%$search%'
UNION 
select * from HABERLER where baslik like '%$search%'

(Muhtemelen kolonlar ve aynı türde aynı sayıda, vb vardır)

Sveyau belirsizdir ve ya aşağıdaki yapılardan birini gerekebilir

SELECT *
FROM mp3
WHERE baslik like '%$search%'"
UNION
SELECT *
FROM HABERLER
WHERE baslik like '%$search%'"

veya

SELECT * 
FROM mp3 M
JOIN HABERLER H on H.some_field = M.some_field_in_mp3_table
WHERE baslik like '%$search%'"

The UNION construct allows one to collect in a single result set recveyads from different tables; the recveyads need to be structurally identical, i.e. have the same column (not necessarily named the same but with the same type of data). The result set includes all the rows that satisfy the first query and all the rows that satisfy the second query (with possible elimination of duplicates depending on the ALL keywveyad).
UNION is useful if fveya example mp3 and HABERLER table contain the same type of recveyads (ex: info about music files).

The JOIN construct allows one to get a result set that contains recveyads which are made from the columns in the first table and the columns made in the second table. The recveyads from the second table are selected on the basis on their satifying the "ON condition" of the JOIN.
JOIN is useful if the two tables contain complementary infveyamation (fveya example mp3 contains info about mp3 files, and HABERLER contains info about musicians; mp3 makes reference to musicians in one of its columns and HABERLER has a column that identify musician by the same value)