我有”
交易”
表:
id user amount currency plan_id
1111usd1
219usd2
3110usd2
4210usd7
517eur3
615eur3
716eur4
833usd8
我想获取按货币排序的金额总和, 然后按每个给定用户的plan_id:$ results [currency] [plan_id]
$results['usd'][1] = 11
$results['usd'][2] = 19
$results['eur'][3] = 12
$results['eur'][4] = 6
到目前为止, 我有:
$query = '
SELECT plan_id, currency, SUM( amount ) as earnings
FROM transactions
WHERE user = 1
GROUP BY currency, plan_id
';
$results = $wpdb->
get_results( $query, OBJECT_K );
但是它只返回按plan_id定期排序的行, 而忽略按货币排序:
Array ( [1] =>
stdClass Object ( [plan_id] =>
1 [currency] =>
usd [earnings] =>
11 ) [2] =>
stdClass Object ( [plan_id] =>
2 [currency] =>
usd [earnings] =>
19 ) [3] =>
stdClass Object ( [plan_id] =>
3 [currency] =>
eur [earnings] =>
12 ) [7] =>
stdClass Object ( [plan_id] =>
7 [currency] =>
eur [earnings] =>
6 ) )
$ wpdb甚至有可能吗?
请帮忙。谢谢
#1你需要告诉MySQL如何使用ORDER BY子句对结果进行排序(查询中缺少)。
【使用WordPress $wpdb获取按2列排序的行结果】这将产生你想要的结果:
SELECT `plan_id`, `currency`, SUM( `amount` ) AS `earnings`
FROM `transactions`
WHERE `user` = 1
GROUP BY `currency`, `plan_id`
ORDER BY `currency` DESC, `plan_id` ASC
推荐阅读
- 在附件页面获取固定链接以获取特色图片
- WP获取分类的链接
- 在WordPress上获取特定SUBCATEGORY的所有帖子()
- 获取特定类别的所有帖子
- 将前端传递来的键值对转换成对象相应的值
- flea-cache使用之Redis集群模式接入
- ES6 第四章 字符串的新增方法
- HarmonyOS ArkUI之自定义组件侧滑菜单(JS)
- 玩转MapStruct,手把手带你学会!