用于组合多个查询输出的存储过程

时间:2017-06-01 15:37:38

标签: mysql database stored-procedures

我有4个数据库表,

session :
------------------------------------------------------
| userid  | session_id   |  sess_type  |  sess_time  |
------------------------------------------------------
| 1       | 365672e3ab75 |  type-1     |  1463214751 |
| 2       | 2612fedcf78d |  type-2     |  1479111234 |
------------------------------------------------------

user :
----------------------------------
| userid  | name   |  user_type  |
----------------------------------
| 1       | Name-1 |  teacher    |
| 2       | Name-2 |  student    |
----------------------------------

teacher :
---------------------------------------------
| userid  | details-col1   |  details-col2  |
---------------------------------------------
| 1       | Value-1        |  Value-1       |
| 2       | Value-2        |  Value-2       |
---------------------------------------------

student :
---------------------------------------------
| userid  | student-col1   |  student-col2  |
---------------------------------------------
| 1       | Value-1        |  Value-1       |
| 2       | Value-2        |  Value-2       |
---------------------------------------------

现在,我的要求是:

  1. 我希望“session”表中的一行与“session_id”字段匹配,并且存储过程参数中提供了session_id值。

  2. 此外,我想在“user”表中添加一行,其中“userid”列值与会话表查询输出中的userid值匹配(指定以上观点:1)。

  3. 现在,根据“user”表查询输出中“user_type”列中存储的值,应该在相应的数据库表上触发查询。

    例如如果“user_type”列中的值为“teacher”,则查询“teacher”表。值“student”的值相同。

  4. 我可以通过在第二个查询中提供一个查询的输出来完成3个不同查询中的上述要求。 但是,我想在一个查询/存储过程中完成它。

    请注意,每个数据库表“user”“teacher”“student”可能包含超过1,000,000行它。表格“老师”“学生”都有超过8个唯一列,并且可能会随时间变化。

    实现此要求的最优化存储过程是什么?

    Expected Output :
    
    For Teacher :
    ---------------------------------------------------------------------------------------------------------------
    | userid  | session_id   |  sess_type  |  sess_time  | name   |  user_type  | details-col1   |  details-col2  |
    ---------------------------------------------------------------------------------------------------------------
    | 1       | 365672e3ab75 |  type-1     |  1463214751 | Name-1 |  teacher    | Value-1        |  Value-1       |
    ---------------------------------------------------------------------------------------------------------------
    
    For Student : 
    ---------------------------------------------------------------------------------------------------------------
    | userid  | session_id   |  sess_type  |  sess_time  | name   |  user_type  | student-col1   |  student-col2  |
    ---------------------------------------------------------------------------------------------------------------
    | 1       | 365672e3ab75 |  type-1     |  1463214751 | Name-1 |  teacher    | Value-1        |  Value-1       |
    ---------------------------------------------------------------------------------------------------------------
    

1 个答案:

答案 0 :(得分:2)

如果需要选择不同的列,可以在存储过程中执行此操作:

SELECT u.user_type, se.session_id, se.sess_type, se.sess_time, u.userid, u.name, u.user_type 
    INTO @user_type, @session_id, @sess_type, @sess_time, @userid, @name, @type
FROM user AS u
JOIN session AS se ON se.userid = u.userid
WHERE se.session_id = @session_id_param;

IF @user_type = 'student'
THEN
    SELECT @userid AS userid, @session_id AS session_id, @sess_type AS sess_type, @sess_time AS sess_time, @name AS name, @type AS user_type, s.student_col1, s.student_col2
    FROM student AS s
    WHERE s.userid = @userid
ELSE
    SELECT @userid AS userid, @session_id AS session_id, @sess_type AS sess_type, @sess_time AS sess_time, , @name AS name, @type AS user_type, t.details_col1, t.details_col2
    FROM teacher AS t
    WHERE t.userid = @userid
END IF;