-1
我試圖提取修改後的歷史記錄字段限制爲100並按每個帳戶分組的總和,但下面的查詢從CDR_Accounts中提取帶有charge_quantity = 60的所有行的總和,並且不會限制到100.GROUP_CONCAT限制爲100個值不起作用
table1 = table2這些是臨時表。我簡化了查詢並過濾了臨時表中的所有數據。
儘管如此,查詢仍然給我所有'歷史'字段的總和,但是我只需要它們中的100個的總和。
表1 /表2格式:
+-----------+----------+------------+-------------+----------------------+
| i_account | id | account_id | CLD | history |
+-----------+----------+------------+-------------+----------------------+
| 10272 | 46479968 | 0814781293 | 27815212963 | [email protected]%=1.32 |
| 6316 | 46480100 | 0813741427 | 27780233136 | [email protected]%=1.32 |
| 6316 | 46480107 | 0813741427 | 27780233136 | [email protected]%=1.32 |
| 13830 | 46480435 | 0814346396 | 27781356515 | [email protected]%=1.32 |
| 13830 | 46480436 | 0814346396 | 27781356515 | [email protected]%=1.32 |
+-----------+----------+------------+-------------+----------------------+
帳戶
SELECT sum(SUBSTRING_INDEX(history,'=',-1)),
cdr.i_account,
cdr.account_id
FROM table1 cdr
WHERE cdr.id IN
(SELECT SUBSTRING_INDEX(group_concat(s.id SEPARATOR ','), ',', 100) id
FROM table2 s
GROUP BY id
)
GROUP BY i_account;
@Tim Biegeleisen目的是限制爲100,無論是使用「限制」還是不使用 – Vadym
我都不認爲group_concat可以以您嘗試的方式使用。 group_concat創建一個逗號分隔的字符串。所以你的起點看起來像這樣:'''''''''('10,20,40,60')'。你可以將這個子選擇改爲'(SELECT charge_quantity FROM CDR_Accounts GROUP BY i_account LIMIT 100)' – CptMisery
@CptMisery在子查詢中的限制在mysql中是不可能的。此外,我已修改查詢,但它仍然給我不期望的結果 – Vadym