如何更改 sql 查询返回的最后一行中的值?
我在编写一个 SQL 查询时遇到问题,该查询返回将特定值放入最后返回行的字段中的行(此修改仅对返回的结果进行,不能对数据库记录进行修改)。
下面是一个示例:
SELECT A.a,
A.b,
A.c
FROM A
WHERE A.b = 10
返回的结果:
A.a |A.b |A.c
--------------
1 10 zaza
2 10 zozo
3 10 zuzu
4 10 zozo
我希望请求自动在最后一行的 Ac 字段中放入特定值,例如:
A.a |A.b |A.c
--------------
1 10 zaza
2 10 zozo
3 10 zuzu
4 10 XXXX
I'm having trouble writing an SQL query that returns rows for which a specific value is put in a field of the last returned row (this modification is only made on the results returned, there must be no modification of the database records).
Here is an example :
SELECT A.a,
A.b,
A.c
FROM A
WHERE A.b = 10
Results returned :
A.a |A.b |A.c
--------------
1 10 zaza
2 10 zozo
3 10 zuzu
4 10 zozo
I would like the request to automatically put a specific value in the A.c field of the last row, such as :
A.a |A.b |A.c
--------------
1 10 zaza
2 10 zozo
3 10 zuzu
4 10 XXXX
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果行的顺序由列
a
确定,则以下内容将替换“最后”行中的C
值。If the order of the rows are determined by column
a
, the following would replace theC
-value in the "last" row.首先,您需要一个 ORDER BY 语句。 Oracle 不保证行将按照输入的顺序返回,因此您需要将 SQL 更改为:
以使“最后”行的概念具有任何意义。
这是一种方法...很可能有比这更好的方法...
如果有 2 行 AA 相同,您不会说行为应该是什么(如果这可能吗?)
Firstly you need an ORDER BY statement. Oracle does not guarantee that rows will be returned in the order that they were entered, so you need to change your SQL to:
for the concept of a "last" row to have any meaning.
Here's one way... there may well be a better way of doing it than this...
You don't say what the behaviour should be if there's 2 rows where A.A are the same (if this is possible?)
稍微不那么混乱:
A bit less messy: