这是我的输出程序中有$将不会打印。 我将使用正确的syntex作为' $' +但它不起作用
SELECT USERS.ID,CONCAT(USERS.FIRSTNAME,' ',USERS.LASTNAME)AS USERNAME,
('$'+ SUM(CPS_HISTORY.CHARGED_AMOUNT+CPS_HISTORY.TRANSACTION_FEE+CPS_HISTORY.SERVICE_CHARGE+CPS_HISTORY.COMBO_PAK_PRICE))AS REVENUE FROM USERS JOIN CPS_HISTORY ON CPS_HISTORY.SUBSCRIBER_ID = USERS.ID GROUP BY USERS.ID ORDER BY REVENUE DESC LIMIT 5;
+---------+---------------+---------+
| ID | USERNAME | REVENUE |
+---------+---------------+---------+
| 1803399 | Michael Rowan | 772 |
| 1697091 | NULL | 676 |
| 1790000 | ree Green | 626 |
| 1766654 | Jose M NUFIO | 625 |
| 1731854 | Ashlee Durgin | 622 |
+---------+---------------+---------+
5 rows in set, 13305 warnings (0.15 sec)
concat函数的问题是:更改数据。
+---------+-----------------+---------+
| ID | USERNAME | REVENUE |
+---------+-----------------+---------+
| 1753814 | Joseph Hearn | $99 |
| 1806377 | Gideon Anderson | $99 |
| 1800992 | Camryn Revitte | $99 |
| 1802344 | Tanner Chik | $99 |
| 1594358 | NULL | $99 |
+---------+-----------------+---------+
5 rows in set (0.00 sec)
答案 0 :(得分:1)
使用concat来连接值:
SELECT USERS.ID,CONCAT(USERS.FIRSTNAME,' ',USERS.LASTNAME)AS USERNAME,
concat('$', SUM(CPS_HISTORY.CHARGED_AMOUNT+CPS_HISTORY.TRANSACTION_FEE+CPS_HISTORY.SERVICE_CHARGE+CPS_HISTORY.COMBO_PAK_PRICE))AS REVENUE
FROM USERS JOIN CPS_HISTORY ON CPS_HISTORY.SUBSCRIBER_ID = USERS.ID GROUP BY USERS.ID ORDER BY REVENUE DESC LIMIT 5;
答案 1 :(得分:0)
数据不会改变,只有类型。
您按照REVENUE
列进行排序,该列之前是数字,但现在,在CONCAT()
之后,它是一个字符串。 $99
作为字符串出现在$772
之前(如果已排序DESC)。
有两种解决方案:
$
而不是SQL,或使用子查询,该查询在内部查询中进行数据拾取和排序,在外部查询中进行格式化。
像
这样的东西SELECT ID, USERNAME, CONCAT('$', REVENUE) AS REVENUE FROM
(SELECT USERS.ID,CONCAT(USERS.FIRSTNAME,' ',USERS.LASTNAME) AS USERNAME,
SUM(CPS_HISTORY.CHARGED_AMOUNT+CPS_HISTORY.TRANSACTION_FEE+CPS_HISTORY.SERVICE_CHARGE+CPS_HISTORY.COMBO_PAK_PRICE)) AS REVENUE
FROM USERS JOIN CPS_HISTORY ON CPS_HISTORY.SUBSCRIBER_ID = USERS.ID
GROUP BY USERS.ID ORDER BY REVENUE DESC LIMIT 5) AS INNER
应该这样做。 (未测试!)