我的查询中的问题,尤其是在这篇文章中:
@a:= concat(@a, ',', B.call_account_id) AS paid_account_id
这是整个查询:
SELECT operator_id, paid_account_ids, SUM( goods_count * price ) AS sales_volume, count(*) AS sales_cnt
FROM (
SELECT B.operator_id, @a:= concat(@a, ',', B.call_account_id) AS paid_account_ids, B.call_time, A.goods_count, A.price, UNIX_TIMESTAMP( A.completion_date ) AS paid_ts
FROM call_module_data B
INNER JOIN ak_accounts A ON ( A.account_id = B.call_account_id AND A.goods_count >=1 )
WHERE B.call_status IN (1,7) AND A.status_id = 5
AND operator_id IN ( $op_ids )
$and_str_accounts
GROUP BY A.account_id
HAVING call_time < (paid_ts + $time_shift)
) AS T
GROUP BY operator_id";
所以上面提到的表达式应该产生连接的帐户 ID 字符串(例如 3341,4355,4433...)。但是我得到了 NULL 而不是所需的字符串。 请帮忙解决。提前致谢。
最佳答案
使用GROUP_CONCAT()函数而不是字符串连接
改变
@a:= concat(@a, ',', B.call_account_id) AS paid_account_id
以上字符串为
GROUP_CONCAT(B.call_account_id) AS paid_account_ids
最终答案:
SELECT operator_id, GROUP_CONCAT(paid_account_ids) AS paid_account_ids,
SUM(goods_count * price) AS sales_volume, COUNT(*) AS sales_cnt
FROM (SELECT B.operator_id, GROUP_CONCAT(B.call_account_id) AS paid_account_ids,
B.call_time, A.goods_count, A.price,
UNIX_TIMESTAMP(A.completion_date) AS paid_ts
FROM call_module_data B
INNER JOIN ak_accounts A ON A.account_id = B.call_account_id AND A.goods_count >=1
WHERE B.call_status IN (1,7) AND A.status_id = 5 AND operator_id IN ($op_ids)
$and_str_accounts
GROUP BY A.account_id HAVING call_time < (paid_ts + $time_shift)
) AS T
GROUP BY operator_id;
关于mysql - 使用 mysql 变量连接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20989376/