gpt4 book ai didi

mysql - 使用 SQL (Grails) 在输出映射中的“SUM()”

转载 作者:行者123 更新时间:2023-11-30 21:59:37 26 4
gpt4 key购买 nike

在我的 MySQL Workbench 中,以下查询完美运行:

SELECT card.number, SUM(qty)
FROM card_set, card, user_card
WHERE card_set.id = card.card_set_id
AND user_card.card_id = card.id
AND card_set.id = 1
GROUP BY card.number ORDER BY card.number;

但是,在我的 Grails Controller 中,我有以下内容:

SELECT card.number, SUM(qty)" +
"FROM card_set, card, user_card\n" +
"WHERE card_set.id = card.card_set_id\n" +
"AND user_card.card_id = card.id\n" +
"AND card_set.id = 1\n" +
"GROUP BY card.number ORDER BY card.number;"

输出是这样的:

[['number':1, 'SUM(qty)':2], ['number':2, 'SUM(qty)':3], ['number':3, 
'SUM(qty)':0], ['number':4, 'SUM(qty)':0], ['number':5, 'SUM(qty)':-2],
['number':6, 'SUM(qty)':0], ['number':7, 'SUM(qty)':2], ['number':8,
'SUM(qty)':2], ['number':9, 'SUM(qty)':0], ['number':10, 'SUM(qty)':0],
['number':11, 'SUM(qty)':1], ['number':12, 'SUM(qty)':0], ['number':13,
'SUM(qty)':1], ['number':14, 'SUM(qty)':1], ['number':15, 'SUM(qty)':2],
['number':16, 'SUM(qty)':-1], ['number':17, 'SUM(qty)':0], ['number':18,
'SUM(qty)':0], ['number':19, 'SUM(qty)':1], ['number':20, 'SUM(qty)':0]]

为什么 Grails 将文本“SUM(qty)”踢出到 map 列表中?我该如何格式化它才能不这样做?

最佳答案

SQL API 和协议(protocol)返回带有每个结果集的列标题元数据。这很方便:它允许 Workbench 和 Grails 等客户端软件确定数据类型和每一列的名称。

但是对于聚合列行 SUM(qty) MySQL 服务器必须猜测名称。聚合列没有来自表的列名。它猜测,嗯,SUM(qty)

如果您将查询的第一行更改为 SELECT card.number, SUM(qty) sum_qty 您将获得更好的结果,因为您已将名称告诉 MySQL(在我的示例中,sum_qty) 你想用于列

关于mysql - 使用 SQL (Grails) 在输出映射中的“SUM()”,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43820478/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com