如何显示每K的大数字(代表千克)?

时间:2015-11-20 11:14:20

标签: php mysql substring

我有一张这样的表:

// table
+----+--------+------------+
| id |  name  | reputation |
+----+--------+------------+
| 1  | jack   | 534431     |
| 2  | peter  | 334        |
| 3  | amos   | 1300       |
| 4  | carter | 13490      |
| 5  | basil  | 1351       |
+----+--------+------------+

现在我想要输出:

// newtable
+----+--------+------------+
| id |  name  | reputation |
+----+--------+------------+
| 1  | jack   | 534k       |
| 2  | peter  | 334        |
| 3  | amos   | 1.3k       |
| 4  | carter | 13.4k      |
| 5  | basil  | 1.3k       |
+----+--------+------------+

嗯,首先,我想知道,我可以使用MySQL做到这一点吗?像这样:

select id, name,
  concat(substr(reputation, 1, 4), IF(LENGTH(reputation) > 4, 'k', '')) as NewRep 
from table

我知道上面的查询不正确,我只是把它作为线索......!

但是如果使用MySQL实现这是不可能的,那么我该如何使用PHP呢?

if (strlen($result['reputation']) >= 4){
    $NewRep = substr($result['reputation'],0,3);
    $NewRep = round($NewRep).'k';
}

然而,这个^解决方案是不完整的。因为它不支持.5 (点半),并且其sbust()也不起作用。

3 个答案:

答案 0 :(得分:4)

SELECT 
    id,
    name,
    IF(reputation >= 1000,
        CONCAT(IF(LENGTH(LEFT(CAST(reputation / 100 AS CHAR), LENGTH(reputation)-2)) >= 4,
                    LEFT(reputation, LENGTH(reputation)-3),
                    LEFT(CAST(reputation / 1000 AS CHAR), LENGTH(reputation)-1)),
                'k'),
        reputation) AS reputation
FROM
    table

这样的事可能会这样做。

<强>输出:

+----+--------+------------+
| id |  name  | reputation |
+----+--------+------------+
| 1  | jack   | 534k       |
| 2  | peter  | 334        |
| 3  | amos   | 1.3k       |
| 4  | carter | 13.4k      |
| 5  | basil  | 1.3k       |
+----+--------+------------+

答案 1 :(得分:2)

我偶然发现了this answer

Renaat De Muynck

我发现了这个解决方案

http://sqlfiddle.com/#!9/666e16/2

SELECT
  id,
  CASE 
   WHEN number = 0 THEN 0 
   ELSE 
    CONCAT(
      ROUND(number / POW(1000, TRUNCATE(LOG(1000, number), 0)), 0),
      ' ',
      ELT(TRUNCATE(LOG(1000, number), 0) + 1, '', 'k', 'M', 'G', 'T', 'P', 'E', 'Z', 'Y', 'B')
    )
  END
  AS number
FROM test;

答案 2 :(得分:0)

试试这个:

<强>查询1:

SELECT id, name, 
   IF(reputation >= 1000,
      CONCAT(
         IF(LENGTH(ROUND(reputation/1000, 1)) > 4,
            ROUND(reputation/1000),
            TRUNCATE(reputation/1000,1)),
      "K"),
   reputation) as reputation  
FROM `table`

查询2: (这更具可读性)

SELECT id, name, 
    CASE WHEN reputation >=100000  THEN  CONCAT( ROUND   ( reputation /1000 ),     "k" ) 
         WHEN reputation >=1000    THEN  CONCAT( TRUNCATE( reputation /1000, 1 ),  "k" ) 
         ELSE reputation
    END AS reputation
FROM `table`

查询3: (更短更快)

SELECT id, name,
    CASE WHEN value >= 1000 THEN
         CONCAT(TRIM(TRAILING '.' FROM SUBSTR(TRUNCATE(number/1000, 1), 1, 4)), 'k')
         ELSE value
    END as reputation
FROM `table` 

输出 (适用于所有查询)

// newtable
+----+--------+------------+
| id |  name  | reputation |
+----+--------+------------+
| 1  | jack   | 534k       |
| 2  | peter  | 334        |
| 3  | amos   | 1.3k       |
| 4  | carter | 13.4k      |
| 5  | basil  | 1.3k       |
+----+--------+------------+