I have a table like this:
// table
+----+--------+------------+
| id | name | reputation |
+----+--------+------------+
| 1 | jack | 534431 |
| 2 | peter | 334 |
| 3 | amos | 1300 |
| 4 | carter | 13490 |
| 5 | basil | 1351 |
+----+--------+------------+
Now I want this output:
// newtable
+----+--------+------------+
| id | name | reputation |
+----+--------+------------+
| 1 | jack | 534k |
| 2 | peter | 334 |
| 3 | amos | 1.3k |
| 4 | carter | 13.4k |
| 5 | basil | 1.3k |
+----+--------+------------+
Well, first of all, I want to know, Is it possible to I do that using MySQL? Something like this:
select id, name,
concat(substr(reputation, 1, 4), IF(LENGTH(reputation) > 4, 'k', '')) as NewRep
from table
I know the above query is not correct, I just said it as a clue ..!
But if implementing that is not possible using MySQL, then how can I do that using PHP?
if (strlen($result['reputation']) >= 4){
$NewRep = substr($result['reputation'],0,3);
$NewRep = round($NewRep).'k';
}
However this ^ solution is incomplete. Because it does not support .5
(point half), and also its sbust()
does not work as well.
Try this:
Query1:
Query2: (This is more readable)
Query3: (shorter and faster)
Output: (for all of queries)
I stumbled upon this answer by Renaat De Muynck
and I found this solution
http://sqlfiddle.com/#!9/666e16/2
Something like that would probably do it.
Output: