2017-01-01 80 views
0

我正在爲我的網頁做搜索,我面臨一個小問題,我想從兩個非關係表中獲取信息,但數據沒有返回我的方式要關於seacher的兩個非關係表的SQL語句

表1

ID |FNAME |LNAME | STATE | CITY 
------------------------------ 
1 |xxxxx1 |xxxxx1| xxxx1 | xx1  
2 |xxxxx2 |xxxxx2| toronto| xx2 
3 |xxxxx3 |xxxxx3| xxxx3 | yy3 
4 |zzzzz3 |zzzzz3| toronto| yy3 

表2

ID |NAME | STATE | CITY 
--------------------- 
1 |yyyyy1 | yyyy1 | yy1  
2 |yyyyy2 | yyyy2 | yy2 
3 |yyyyy3 | toronto| yy3 
目前

我哈已經

SELECT 
    e.id_client, e.fname_client, e.city_client, e.state_client , m.id_client, m.fname_client, m.lname_client, m.state_client, m.city_client 
FROM 
    empresas e 
CROSS JOIN 
    medicos m 
WHERE 
    e.fname_client LIKE :busqueda 
OR 
    e.city_client LIKE :busqueda 
OR 
    m.fname_client LIKE :busqueda 
OR 
    m.lname_client LIKE :busqueda 
OR 
    m.state_client LIKE :busqueda 
OR 
    m.city_client LIKE :busqueda 

這顯示這樣

ID| FNAME |LNAME | STATE | CITY |ID|FNAME |LNAME | STATE | CITY 
3 |yyyyy3 |  | yyyy3 | yy3 |3 |xxxxx3 |xxxxx3| xxxx3 | yy3 

的結果,但我希望它這樣

ID|FNAME |LNAME | STATE | CITY 
3 |xxxxx3 |xxxxx3| xxxx3 | yy3 
3 |yyyyy3 |  | yyyy3 | yy3 

編輯:

與UNION asnwer我得到的數據我想要的格式,但是當我搜索公共值時只顯示一個表格的結果,例如:

如果鍵入「多倫多」,這一點必須說明

ID|FNAME |LNAME | STATE | CITY 
    2 |xxxxx3 |xxxxx3| toronto | yy3 <- doctor 
    3 |yyyyy3 |  | toronto | yy3 <- organization 
    4 |zzzzz3 |zzzzz3| toronto | yy3 

,但只顯示該

ID|FNAME |LNAME | STATE | CITY 
    2 |xxxxx3 |xxxxx3| toronto | yy3 <- doctor 
    4 |zzzzz3 |zzzzz3| toronto | yy3 <- doctor 
+1

你可以做的,而不是一個交叉聯接UNION操作。 – markg

回答

0

我猜你想串聯兩個表。然後......

select 
    id, fname, lname, state, city 
from 
    table1 
where 
    <your where condition here> 
union all 
select 
    id, fname, lname, state, city 
from 
    table2 
where 
    <your other where condition here> 

編輯

如果您有:

SQL> select * from table1 order by id; 
id | fname | lname | state | city  
----+------------+------------+------------+------------ 
    1 | xxxxx1  | xxxxx1  | xxxx1  | xx1  
    2 | xxxxx2  | xxxxx2  | toronto | xx2  
    3 | xxxxx3  | xxxxx3  | xxxx3  | xx3  
    4 | zzzzz3  | zzzzz3  | toronto | yy3 
SQL> select * from table2 order by id; 
id | name | state | city  
----+------------+------------+------------ 
    1 | yyyyy1  | yyyy1  | yy1  
    2 | yyyyy2  | yyyy2  | yy2  
    3 | yyyyy3  | toronto | yy3 

則:

select 
    id, fname, lname, state, city 
from 
    table1 
where 
    state='toronto' 
union all 
select 
    id, name as fname, NULL as lname, state, city 
from 
    table2 
where 
    state='toronto' 
order by id; 

id | fname | lname | state | city  
----+------------+------------+------------+------------ 
    2 | xxxxx2  | xxxxx2  | toronto | xx2  
    3 | yyyyy3  | (null)  | toronto | yy3  
    4 | zzzzz3  | zzzzz3  | toronto | yy3  
+0

這幾乎完美的工作,但是,當我尋找共同的結果時,它只顯示了medicos的結果,可能需要應用一個外連接? –

+0

我猜empresas上的選擇不會返回任何行與您的where條件...嘗試自行運行它。順便說一句,你的意思是「共同的結果」?請編輯您的問題,爲您的兩張表添加樣本數據以及預期結果。 – mauro

+0

我的意思是,例如,如果我輸入多倫多,這必須顯示所有結果與該單詞,但它只顯示醫生不是公司,但查詢工程,我運行它,並很好地重新運行的價值。要編輯問題 –

0

我剪了,並從原來的查詢粘貼,所以希望沒有太多的錯別字!顯然,我無法測試它,但它應該可以工作。您需要填寫第二個查詢中剩餘的列,否則它們將不匹配,因爲第一個查詢中的額外名稱字段。

(SELECT m.id_client, m.fname_client, m.lname_client, m.state_client, m.city_client FROM medicos m WHERE m.fname_client LIKE :busqueda OR m.lname_client LIKE :busqueda OR m.state_client LIKE :busqueda OR m.city_client LIKE :busqueda) UNION (SELECT e.id_client, e.fname_client, '', e.city_client, e.state_client FROM empresas e WHERE e.fname_client LIKE :busqueda OR e.city_client LIKE :busqueda) 
+0

這幾乎完美的工作,但是,當我尋找共同的結果時,它只顯示了medicos的結果,可能需要應用一個外連接? –

+0

嗨。感謝您的反饋。它不應該需要聯接,因爲它們是兩個單獨的查詢,您只需要合併結果。您是否嘗試運行第二個查詢以確保它自己返回記錄? – Nick

+0

它只是發生在我身上...如果記錄最終與兩個表相同,它將只包含一個實例 - 即DISTINCT是默認值。將UNION關鍵字後面的ALL全部檢查一下。 – Nick