2013-01-13 34 views
0

我想在我的網站的搜索功能獲取disting行。從多個表

我想允許基於用戶名,城市,學院/學校名稱,employeer搜索。 名稱是強制性的,其他都是可選字段。 我有以下各表

users(id,fname,lname,city) 
profile_pic(id,pic,userid)//userid from users(id) 
educationdetail(eduid,school,userid)//userid from users(id) 
employment(empid.employeer,userid)//userid from users(id) 

在每個用戶profile_pic是僅具有一個圖像; 在educationdetail用戶可以在就業用戶添加多個學校大學細節 可以添加多個工作細節

搜索目的,我使用下面的查詢:

SELECT distinct s.id, s.fname, s.lname,pr.pic 
    FROM users as s LEFT JOIN profile_pic as pr 
    on s.id = pr.userid 
    LEFT JOIN educationdetail as edu 
    on s.id=edu.userid 
    LEFT JOIN employment as emp 
    on s.id=emp.userid 
    where s.fname like '%m%' and s.lname like '%%%' and s.city like '%%%' 

即給予適當的細節

但是當包括很少的參數,它沒有顯示任何結果

 SELECT distinct s.id, s.fname, s.lname,pr.pic 
    FROM users as s LEFT JOIN profile_pic as pr 
    on s.id = pr.userid 
    LEFT JOIN educationdetail as edu 
    on s.id=edu.userid 
    LEFT JOIN employment as emp 
    on s.id=emp.userid 
    where s.fname like '%m%' and s.lname like '%%%' and s.city like '%%%' 
    and edu.school='%%%' and emp.employeer='%%%'; 

顯示

Empty set (0.00 sec) 
+0

你說的'LIKE '%%%''是什麼意思? –

+0

'%pattern%'此處'%%%'表示任何模式 – xrcwrn

回答

0

也許你可以拍打使用搜索時使用OR,然後LIKE=

and edu.school LIKE '%' and emp.employeer LIKE '%'; 

更新1

它不返回任何值的原因是因爲查詢正在搜索的%%%領域edu.school不存在的價值。

+0

您是正確的,但現在也和edu.school LIKE'%'和emp.employeer LIKE'%'不顯示結果 – xrcwrn

+0

您嘗試的值是什麼尋找? –

+0

s.fname like'%man%'and rest like like'%'它不提取數據,但提問中提到的1個查詢顯示所有相關數據 – xrcwrn

0

您必須使用ESCAPE關鍵字,指定一個轉義字符,試圖在你的情況與謂詞LIKE來搜索保留通配符,如果你要搜索的值%,使用時:

WHERE name LIKE '%|%%' ESCAPE '|' -- Or any escape character. 

,而不是隻:

WHERE name LIKE '%%%'