在PostgreSQL中,如何将SELECT语句返回的数据集中的BIT值替换为STRING(字,无数字)?一般而言,我对SQL还是很陌生,所以我的经验和Google搜索并没有被证明过。
在这种情况下,c.visible是一个存储0(可见)或1(不可见)的位值。在我的查询中,我想显示c.visible = 0的所有结果,但显示“该课程对学生可见”。而不是此select语句返回的数据集中的“ 0”。我尝试将其强制转换为SELECT语句中的文本,然后更改WHERE子句中CASE WHEN语句中的值,但这会导致无效的输入语法错误。 c.visibility是数据库中记录此信息的唯一属性。
例如:
--------------------------------------------------------
| CourseName | ID | CourseVisibility |
--------------------------------------------------------
| ABC | 10 | This course is visible to students.|
--------------------------------------------------------
| DEF | 22 | This course is visible to students.|
--------------------------------------------------------
SELECT c.fullname AS CourseName, c.id AS ID, CAST(c.visible AS TEXT) AS CourseVisibilty
FROM prefix_course c, prefix_logstore_standard_log lsl
WHERE
c.visible = 0
CASE WHEN c.visible = '0'
THEN c.visible = 'This course is visible to students.'
END
AND lsl.courseid = c.id
AND lsl.timecreated BETWEEN extract(epoch from NOW()- INTERVAL '12 month') AND extract(epoch from NOW())
GROUP BY c.fullname, c.id, c.visible
ERROR: invalid input syntax for integer: "This course is visible to students."
LINE 5: THEN c.visible = 'This course is visible to students.'
^
执行此操作的正确方法是什么?
答案 0 :(得分:2)
您不能在where
子句中修改返回的值,但是可以在select
子句中进行修改
SELECT
c.fullname AS CourseName,
c.id AS ID,
CASE WHEN c.visible = '0'
THEN 'This course is visible to students.'
ELSE 'This course is not visible to students.'
END AS CourseVisibilty
FROM prefix_course c, prefix_logstore_standard_log lsl
WHERE
lsl.courseid = c.id
AND lsl.timecreated BETWEEN extract(epoch from NOW()- INTERVAL '12 month') AND extract(epoch from NOW())
GROUP BY c.fullname, c.id, c.visible