2015-01-21 39 views
0

這是我的程序在輸出中有$不會打印。 我會用正確的了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

將upcase更改爲locase。 – 2015-01-21 05:57:44

+0

@AvinashRaj然後呢? – glglgl 2015-01-21 07:03:39

+0

對所有的downvoters:有什麼意義?你不明白這個問題嗎? – glglgl 2015-01-21 07:04:11

回答

1

使用的concat到concatinate的值:

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; 
+0

它將打印不正確的答案。總和的值被改變。並在所有行中保持相同。所以concat()函數有問題。 – 2015-01-21 06:31:29

+0

你能否在你的問題和這兩個表格的表格描述中添加一些示例數據? – Jens 2015-01-21 06:36:56

+0

我在我的問題中添加它。 – 2015-01-21 06:56:57

0

的數據不改變,只有類型。

您正在通過您的REVENUE列進行訂購,該列是之前的數字,但現在在CONCAT()之後,它是一個字符串。而$99作爲字符串出現在$772之前(如果排序DESC)。

解決辦法有兩個:

  1. 添加在應用程序中$相反在SQL或
  2. 與子查詢其數據是否採摘和工作排序在內部查詢和格式化外面的一個。

    喜歡的東西

    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 
    

    應該做的。 (未經測試!)

相關問題