我从 postgres 开始,我有一个关于选择查询中的多个选择案例的问题。
bd_david=> select * from edt_sem;
code_module | groupe | week | day | hour | room
-------------+----------+------+-----+--------+-------
M3106 | INFOFCAS | 1 | 1 | 1 | O104
M3105 | INFOFCAS | 1 | 1 | 5 | O104
M3106 | INFOFCAS | 2 | 1 | 1 | O104
(3 rows)
我想要做的是创建一个只显示某一周的行的函数,为此我正在使用这个函数:
CREATE or REPLACE function show_edt(IN paramSem numeric)
RETURNS TABLE(module varchar, groupe varchar, week numeric, day text, hour text, room varchar) AS
$$
BEGIN
RETURN QUERY
SELECT e.code_module,
e.groupe,
e.sem,
(SELECT CASE
WHEN e.jour = 1 THEN 'Monday'
WHEN e.jour = 2 THEN 'Tuesday'
WHEN e.jour = 3 THEN 'Wednesday'
WHEN e.jour = 4 THEN 'Thursday'
WHEN e.jour = 5 THEN 'Friday'
END "JOUR"
FROM edt_sem WHERE edt_sem.sem = paramSem [ LIMIT 1... ? Don't know ] ),
(SELECT CASE
WHEN heured = 1 THEN '9h'
WHEN heured = 2 THEN '10h'
WHEN heured = 3 THEN '11h'
WHEN heured = 4 THEN '12h'
WHEN heured = 5 THEN '14h'
WHEN heured = 6 THEN '15h'
WHEN heured = 7 THEN '16h'
WHEN heured = 8 THEN '17h'
END "HEURE"
FROM edt_sem WHERE edt_sem.sem = paramSem [ LIMIT 2 ?? don't know.. ]),
e.salle
FROM edt_sem e where e.sem = paramSem;
END;
$$ LANGUAGE plpgsql;
它适用于单行:
select * from afficher_edt(2);
module | groupe | week | day | hour | room
--------+----------+---------+-------+-------+-------
M3106 | INFOFCAS | 2 | Monday| 9h | O104
(1 row)
但我有不止一行的问题:
bd_david=> select * from afficher_edt(1);
module | groupe | week | day | hour | room
--------+----------+---------+-------+-------+-------
M3106 | INFOFCAS | 1 | Lundi | 9h | O104
M3105 | INFOFCAS | 1 | Lundi | 9h /!\| O104
/!\ 它应该显示为 14h 而不是 9h
编辑:错误信息:
bd_david=> select * from show_edt(1);
ERROR: more than one row returned by a subquery used as an expression
CONTEXT: PL/pgSQL function afficher_edt(numeric) line 3 at RETURN QUERY
好吧,我或多或少地知道为什么会出现此错误:这是因为我的选择案例在不同时间提取了两行,但我不知道如何摆脱它..:/