存储过程mysql中的多选语句

Posted

技术标签:

【中文标题】存储过程mysql中的多选语句【英文标题】:multi select statement inside a stored procedure mysql 【发布时间】:2013-07-29 05:03:51 【问题描述】:

我的 dbpre_exer5 中有这些表;//mysql wampserver2.2

+-----------------------+
| Tables_in_dbpre_exer5 |
+-----------------------+
| tblcourse             |
| tblcutoff             |
| tblgrades             |
| tblstud               |
| tblsub                |
+-----------------------+

现在,我只想问一下,考虑到存储过程的使用情况,我将如何获得这些输出。 输出是:

+---------+----------------+---------------+-------+
| Gender  |  With Failure  | W/out Failure | Total |
+---------+----------------+---------------+-------+
| Male    |       1        |       1       |   2   |
| Female  |       2        |       1       |   3   |
+---------+----------------+---------------+-------+

顺便说一下这里是enter code here; **

mysql> select * from tblcourse;
+-----------+------------------------+
| course_id | course_name            |
+-----------+------------------------+
|         1 | Information Technology |
|         2 | Computer Science       |
+-----------+------------------------+
2 rows in set (0.00 sec)

mysql> select * from tblgrades;
+---------+--------+-------+
| stud_id | sub_id | grade |
+---------+--------+-------+
|       1 |      1 |    80 |
|       1 |      2 |    78 |
|       2 |      2 |    75 |
|       2 |      3 |    84 |
|       3 |      1 |    81 |
|       3 |      3 |    90 |
|       4 |      1 |    74 |
|       4 |      2 |    77 |
|       5 |      2 |    76 |
|       5 |      3 |    81 |
+---------+--------+-------+
10 rows in set (0.00 sec)

mysql> select * from tblcourse;
+-----------+------------------------+
| course_id | course_name            |
+-----------+------------------------+
|         1 | Information Technology |
|         2 | Computer Science       |
+-----------+------------------------+
2 rows in set (0.00 sec)

mysql> select * from tblcutoff;
+-----------+
| passgrade |
+-----------+
|        78 |
+-----------+
1 row in set (0.00 sec)

mysql> select * from tblgrades;
+---------+--------+-------+
| stud_id | sub_id | grade |
+---------+--------+-------+
|       1 |      1 |    80 |
|       1 |      2 |    78 |
|       2 |      2 |    75 |
|       2 |      3 |    84 |
|       3 |      1 |    81 |
|       3 |      3 |    90 |
|       4 |      1 |    74 |
|       4 |      2 |    77 |
|       5 |      2 |    76 |
|       5 |      3 |    81 |
+---------+--------+-------+
10 rows in set (0.00 sec)

mysql> select * from tblstud;
+---------+-------------------+--------+-----------+
| stud_id | stud_name         | gender | course_id |
+---------+-------------------+--------+-----------+
|       1 | Angelina Jolie    | F      |         1 |
|       2 | Jennifer Garner   | F      |         1 |
|       3 | Liam Neeson       | M      |         2 |
|       4 | Paul Walker       | M      |         2 |
|       5 | Jennifer Lawrence | F      |         2 |
+---------+-------------------+--------+-----------+
5 rows in set (0.00 sec)

mysql> select * from tblsub;
+--------+------------+
| sub_id | sub_name   |
+--------+------------+
|      1 | Math 1     |
|      2 | English 1  |
|      3 | Filipino 1 |
+--------+------------+
3 rows in set (0.00 sec)

mysql>

**

我的第一个问题是在性别下得到“男性”和“女性”的结果。 有什么帮助吗?非常感谢。

【问题讨论】:

@Luv 我无法在存储过程中创建一个函数..我只是通过 select 语句做到了,但输出仍然是错误的 有没有办法从 3 个或多个表中获取值? 复制:***.com/questions/4706100/… 【参考方案1】:

更新

SELECT gender, 
       SUM(failure) `With failure`,
       COUNT(*) - SUM(failure) `Without failure`,
       COUNT(*) total
  FROM
(
  SELECT s.stud_id, 
         CASE WHEN s.gender = 'M' THEN 'Male' ELSE 'Female' END gender,
         MAX(CASE WHEN g.grade <  c.passgrade THEN 1 ELSE 0 END) failure
    FROM tblgrades g JOIN tblstud s 
      ON g.stud_id = s.stud_id CROSS JOIN tblcutoff c
   GROUP BY s.stud_id
) q
 GROUP BY gender

示例输出:

|性别 |失败 |没有失败|总计 | -------------------------------------------------- - |女 | 2 | 1 | 3 | |男 | 1 | 1 | 2 |

这里是SQLFiddle演示

【讨论】:

是的先生..但是失败的计数应该是唯一的..student_id 采用subj_id ..这意味着失败计数也必须与主题..stud_id&&subj_id 结合起来 @user2628953 它已经以这种方式“集成”在tblgrades 表中,不是吗?如果不是,请详细说明您的示例数据,什么应该和什么不应该被视为失败,我们可以修复查询。 say stud_id 4 has failed sub_id 1 和 2,查询应该只将此失败计为 1 .. 计数不取决于 stud_id 失败的主题数量,而是取决于 stud_id 是否有失败然后计数【参考方案2】:

你可以使用一个简单的case语句:

CASE WHEN 'F' THEN SELECT 'Female' ELSE SELECT 'Male'

Further reading

【讨论】:

以上是关于存储过程mysql中的多选语句的主要内容,如果未能解决你的问题,请参考以下文章

MySQL中的存储过程(详细篇)

mysql中的存储过程怎么使用

Birt报表存储过程多选参数的设置

mysql中如何创建存储过程

mysql 存储过程的实现原理

MySQL数据库中的高级(进阶)正则和存储过程