1

我必须将表格设置为:table1:

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

表2:

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

我有 2 个查询:

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

我想在一个过程中编写这两个查询。

4

3 回答 3

2

单个过程中多个选择查询的结构:

CREATE PROCEDURE样本(l_uid INT)BEGIN

SELECT* FROMCollege_edu WHEREuid=l_uid;

SELECT* FROMwork_experience WHEREuid=l_id;

END

于 2013-02-22T11:19:53.663 回答
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,请删除 上的ALL关键字UNION

于 2013-02-22T09:37:27.553 回答
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
于 2013-02-22T09:40:39.763 回答