2013-02-12 131 views
0

我使用查詢像這樣得到導師和研究機構。過濾查詢結果

SELECT 
    t.tutor_id member_id, 
    t.tutor_name member_name, 
    t.tutor_code member_code, 
    'tutor' AS TYPE 
FROM 
    tutors t 
WHERE t.tutor_name LIKE '%jaya%' 
UNION 
SELECT 
    t.institute_id, 
    t.institute_name, 
    t.institute_code, 
    'institute' AS TYPE 
FROM 
    institute i 
WHERE i.institute_name LIKE '%jaya%' 

該查詢根據給定關鍵字返回記錄(導師和研究機構)。它爲我工作。我的問題是我需要單獨保存記錄,因爲導師和研究機構從查詢中選擇所有記錄。我在頁面上使用了名爲'tutors'和'institute'的2個按鈕來過濾查詢結果。所以現在我需要知道我需要使用2級不同的查詢爲每個按鈕或者如果不是我可以使用單個查詢做到這一點的?

任何意見都大大升值。 謝謝。

+3

使用單獨查詢更簡單,更高效 – 2013-02-12 07:26:13

+0

共享'tutors'和'institute'表的表格模式。 – SparKot 2013-02-12 07:26:26

+0

你能告訴我我如何共享表模式? – TNK 2013-02-12 07:30:11

回答

0

當一個按鈕讓假設導師點擊發送兩個指標的影響,以PHP函數

function searchTutors($type , $keyword) 
{ 
    if($type == 'tutors') 
    { 
     //tutors query here 
    }else{ 
     //institutions query here 
    } 
    return $your_query_result; 
} 
0

如果您希望只運行一個查詢,你可以有不同的看法。

$type=$_POST['submit']; 

if($type=='tutor') 
{ 
    // show only columns from tutor table 
} 
else 
{ 
// show only columns from institute table 
}