2013-02-22 80 views
1

我要表爲: 表1:如何在單個程序中編寫多個選擇查詢?

UID | COLLEGE_NAME | COLLEGE_ADDRESS 
------------------------------------ 

表2:

UID | COMPANY_NAME | COMPANY_ADDRESS 
------------------------------------ 

我有2個疑問:

select * from table1 where uid='$uid'; 
select * from table2 where uid='$uid'; 

我想在一個程序寫這兩個查詢。

+1

你想他們的結果結合起來? – 2013-02-22 09:35:33

+0

雅我必須結合的結果也。 – 2013-02-22 09:36:26

回答

0

以下是創建STORED PROCEDURE的聲明。

DELIMITER $$ 
CREATE PROCEDURE procedureName(IN _uid VARCHAR(15)) 
BEGIN 
    SELECT UID, COLLEGE_NAME name, COLLEGE_ADDRESS address 
    FROM table1 
    WHERE uid = _uid 
    UNION ALL 
    SELECT UID, COMPANY_NAME name, COMPANY_ADDRESS address 
    FROM table2 
    WHERE uid = _uid 
END $$ 
DELIMITER ; 

通知,UNIONALL關鍵字就可以在結果列表上添加重複的記錄。但如果您更喜歡UNIQUE,請刪除UNION上的ALL關鍵字。

+1

如果我不想聯合結果,我只想繼承過程中的多個select查詢呢? – 2013-02-22 10:01:32

0

以下代碼可能爲您服務。此外,額外的tbl列會讓你知道你的數據來自哪個表,這將有助於進一步操作。

SELECT UID, COLLEGE_NAME name, COLLEGE_ADDRESS address , 1 as tbl 
    FROM table1 
    WHERE uid = _uid 
    UNION ALL 
    SELECT UID, COMPANY_NAME name, COMPANY_ADDRESS address , 2 as tbl 
    FROM table2 
    WHERE uid = _uid 
+0

如果我不想結合結果,我只想繼承過程中的多個select查詢呢? – 2013-02-22 10:04:13

2

結構爲在單個過程中多個選擇奎雷斯:

CREATE PROCEDURE樣品(l_uid INT)BEGIN

SELECT * FROM college_edu WHERE UID = l_uid;

SELECT * FROM work_experience WHERE uid = l_id;

END

相關問題