我有一张计算百分比的表,
+------+---------+-------------------+
| id1 | id2 |percentage(%) |
+------+---------+-------------------+
| 4 | 6 | 66.6667 |
| 4 | 5 | 80.0000 |
+------+---------+-------------------+
我想在逗号后删除数字,所以结果就像这样
+------+---------+-------------------+
| id1 | id2 |percentage(%) |
+------+---------+-------------------+
| 4 | 6 | 66 |
| 4 | 5 | 80 |
+------+---------+-------------------+
感谢您的帮助,掌握。
答案 0 :(得分:0)
select id1,id2,round(percentage) as percentage(%) from table
试试这个..
答案 1 :(得分:0)
您可以根据自己的要求使用ROUND
,FLOOR
和CEIL
功能。
您也可以在插入带有其他列上的表达式的结果列时使用它们。
示例:INSERT :
-- this results a 67
INSERT INTO my_table( a, b, p ) VALUES( 4, 6, ROUND( ( 4 * 100 ) / 6 ) );
-- alternatively
INSERT INTO my_table( a, b, p ) VALUES( 4, 6, ROUND( ( a * 100 ) / b ) );
使用上述语句,您可以限制分数部分不存储在列中。如果您仍然需要它,您可以指定小数部分的位数,例如2
。
-- this results a 66.67
INSERT INTO my_table( a, b, p ) VALUES( 4, 6, ROUND( ( 4 * 100 ) / 6 ), 2 );
-- alternatively
INSERT INTO my_table( a, b, p ) VALUES( 4, 6, ROUND( ( a * 100 ) / b ), 2 );
示例:SELECT :
SELECT 4 AS 'a', 6 AS 'b',
( 4 * 100 ) / 6 AS 'prcntge(%)',
ROUND( ( 4 * 100 ) / 6, 0 ) AS 'rndd(%)',
FLOOR( ( 4 * 100 ) / 6 ) AS 'flr(%)',
CEIL( ( 4 * 100 ) / 6 ) AS 'ceil(%)'
UNION ALL
SELECT 5 AS 'a', 6 AS 'b',
( 5 * 100 ) / 6 AS 'prcntge(%)',
ROUND( ( 5 * 100 ) / 6, 0 ) AS 'rndd(%)',
FLOOR( ( 5 * 100 ) / 6 ) AS 'flr(%)',
CEIL( ( 5 * 100 ) / 6 ) AS 'ceil(%)'
UNION ALL
SELECT 4 AS 'a', 5 AS 'b',
( 4 * 100 ) / 5 AS 'prcntge(%)',
ROUND( ( 4 * 100 ) / 5, 0 ) AS 'rndd(%)',
FLOOR( ( 4 * 100 ) / 5 ) AS 'flr(%)',
CEIL( ( 4 * 100 ) / 5 ) AS 'ceil(%)'
以上查询的结果:
+---+---+------------+---------+--------+---------+
| a | b | prcntge(%) | rndd(%) | flr(%) | ceil(%) |
+---+---+------------+---------+--------+---------+
| 4 | 6 | 66.6667 | 67 | 66 | 67 |
| 5 | 6 | 83.3333 | 83 | 83 | 84 |
| 4 | 5 | 80.0000 | 80 | 80 | 80 |
+---+---+------------+---------+--------+---------+