
- Learn MySQL
- MySQL - Home
- MySQL - Introduction
- MySQL - Installation
- MySQL - Administration
- MySQL - PHP Syntax
- MySQL - Connection
- MySQL - Create Database
- MySQL - Drop Database
- MySQL - Select Database
- MySQL - Data Types
- MySQL - Create Tables
- MySQL - Drop Tables
- MySQL - Insert Query
- MySQL - Select Query
- MySQL - Where Clause
- MySQL - Update Query
- MySQL - Delete Query
- MySQL - Like Clause
- MySQL - Sorting Results
- MySQL - Using Join
- MySQL - NULL Values
- MySQL - Regexps
- MySQL - Transactions
- MySQL - Alter Command
- MySQL - Indexes
- MySQL - Temporary Tables
- MySQL - Clone Tables
- MySQL - Database Info
- MySQL - Using Sequences
- MySQL - Handling Duplicates
- MySQL - SQL Injection
- MySQL - Database Export
- MySQL - Database Import
Selecting and dividing numbers in a column and displaying the decimal result in integer with MySQL
Let us first create a table −
mysql> create table DemoTable -> ( -> Id int NOT NULL AUTO_INCREMENT PRIMARY KEY, -> Number int -> ); Query OK, 0 rows affected (0.50 sec)
Insert some records in the table using insert command −
mysql> insert into DemoTable(Number) values(10); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable(Number) values(20); Query OK, 1 row affected (0.12 sec) mysql> insert into DemoTable(Number) values(30); Query OK, 1 row affected (0.20 sec) mysql> insert into DemoTable(Number) values(40); Query OK, 1 row affected (0.14 sec)
Display all records from the table using select statement −
mysql> select *from DemoTable;
Output
This will produce the following output −
+----+--------+ | Id | Number | +----+--------+ | 1 | 10 | | 2 | 20 | | 3 | 30 | | 4 | 40 | +----+--------+ 4 rows in set (0.00 sec)
Here is the query to select an int and dividing using MySQL −
mysql> select Id,Number,(Number/2) AS DivideBy2 from DemoTable;
Ouptut
This will produce the following output. The division result is in decimal values −
+----+--------+-----------+ | Id | Number | DivideBy2 | +----+--------+-----------+ | 1 | 10 | 5.0000 | | 2 | 20 | 10.0000 | | 3 | 30 | 15.0000 | | 4 | 40 | 20.0000 | +----+--------+-----------+ 4 rows in set (0.00 sec)
If you do not want decimal value, the use the following query and convert in integer −
mysql> select Id,Number,cast((Number/2) AS UNSIGNED) AS DivideBy2 from DemoTable;
Ouptut
This will produce the following output −
+----+--------+-----------+ | Id | Number | DivideBy2 | +----+--------+-----------+ | 1 | 10 | 5 | | 2 | 20 | 10 | | 3 | 30 | 15 | | 4 | 40 | 20 | +----+--------+-----------+ 4 rows in set (0.00 sec)
- Related Articles
- Selecting and displaying only some rows from a column in a MySQL table
- Divide numbers from two columns and display result in a new column with MySQL
- Count duplicate ids and display the result in a separate column with MySQL
- Selecting Random Result from MySQL?
- How to round MySQL column with float values and display the result in a new column?
- MySQL query to sum 3 different values in a column displaying total of each value in result set?
- Calculate average of column values and display the result with no decimals in MySQL
- Truncate results in decimal to integer value with MySQL
- MySQL Select displaying Data type in a separate column?
- Update existing column data in MySQL and remove the last string from a varchar column with strings and numbers
- Selecting a column that is also a keyword in MySQL?
- Sort character values from a column mixed with character and numbers in MySQL?
- Display the count of duplicate records from a column in MySQL and order the result
- Creating and Selecting a MySQL Database
- Sort a list in MySQL and display a fixed result at the end of the column?

Advertisements