在 postgresql 中将整数转换为字符串
本教程讨论如何在 postgresql 中将整数转换为字符串。
在 postgresql 中将整数转换为字符串
考虑一个 quiz_score
表,它记录了问答游戏中每个参与者的分数。分数作为字符串而不是整数存储在此表中。
id | player_id | score |
---|---|---|
1 | 1 | 54 |
2 | 2 | 72 |
3 | 3 | 52 |
4 | 4 | 55 |
5 | 5 | 93 |
6 | 6 | 72 |
7 | 7 | 55 |
8 | 8 | 64 |
9 | 9 | 87 |
10 | 10 | 81 |
下面是表的 create
语句:
createtablequiz_score(idintegernotnullgeneratedalwaysasidentity,player_idintegernotnull,scoretextnotnull,constraintquiz_score_pkeyprimarykey(id));
这是用数据填充表的 insert
语句:
insertintoquiz_score(player_id,score)values(1,54),(2,72),(3,52),(4,55),(5,93),(6,72),(7,55),(8,64),(9,87),(10,81);
如果我们被要求找到一个或多个有特定分数的球员,而我们得到的这个分数是整数类型而不是存储在表中的字符串类型,这样的查询:
select*fromquiz_scorewherescore=72;
会给出以下错误:
error: operator does not exist: text = integer
hint: no operator matches the given name and argument types. you might need to add explicit type casts.
按照给出的提示,我们必须将给定的分数(整数类型)转换为字符串,如下所示:
select*fromquiz_scorewherescore=72::text;
没有抛出错误,结果如下:
id | player_id | score |
---|---|---|
2 | 2 | 72 |
6 | 6 | 72 |
将整数转换为字符串的另一种方法如下:
select*fromquiz_scorewherescore=cast(72astext);
本教程讨论了将整数转换为字符串的两种方法。
转载请发邮件至 1244347461@qq.com 进行申请,经作者同意之后,转载请以链接形式注明出处
本文地址:
相关文章
在一个 postgresql 查询中使用多个 with 语句
发布时间:2023/03/20 浏览次数:337 分类:postgresql
-
在本教程中,我们将学习如何使用多个 with 语句在 postgresql 中使用两个临时表执行查询。
发布时间:2023/03/20 浏览次数:185 分类:postgresql
-
本文介绍如何在 ubuntu 上找到 postgresql 数据库的配置文件。
发布时间:2023/03/20 浏览次数:409 分类:数据库
-
本文解释了如何直接从终端/命令行或 psql shell 运行 sql 文件。为此,你需要指定主机名、端口、用户名和数据库名称。
发布时间:2023/03/20 浏览次数:89 分类:postgresql
-
本文展示了如何列出 postgresql 上的活动连接。
发布时间:2023/03/20 浏览次数:966 分类:postgresql
-
在 pl/sql 中,你可能需要在 postgres 中使用循环。我们可以使用 for 和 while 语句来创建循环。
发布时间:2023/03/20 浏览次数:141 分类:postgresql
-
本文介绍如何在 postgresql 中仅使用单个查询来重命名列以及更改其类型。
发布时间:2023/03/20 浏览次数:233 分类:postgresql
-
本文介绍如何在 postgresql 中使用 select 方法连接列。
发布时间:2023/03/20 浏览次数:281 分类:postgresql
-
本文展示了如何使用 case 语句并给出了 postgresql 中的示例。