Home >Database >Mysql Tutorial >How to Get a Real-Valued Result from Integer Division in SQLite?

How to Get a Real-Valued Result from Integer Division in SQLite?

DDD
DDDOriginal
2024-12-15 14:51:15885browse

How to Get a Real-Valued Result from Integer Division in SQLite?

Converting Integer Division to Real in SQLite

In SQLite, division of two integers returns an integer value. This can be an issue when you need to obtain a real-valued result.

Example:

Consider the following query:

SELECT totalUsers/totalBids FROM
(SELECT (SELECT COUNT(*) FROM Bids) AS totalBids,
(SELECT COUNT(*) FROM Users) AS totalUsers) A;

This query returns 1 as the division result, which is an integer.

Solution:

To obtain a real-valued result, you can explicitly cast one of the numbers to a real number. This can be done by multiplying it by 1.0.

Modified Query:

SELECT something*1.0/total FROM somewhere

This modification ensures that the division operation returns a floating-point result.

The above is the detailed content of How to Get a Real-Valued Result from Integer Division in SQLite?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn