Hello and welcome to our community! Is this your first visit?
Enjoy an ad free experience by logging in. Not a member yet? Register.

# Thread: ratio of row1 and row 2

1. ## ratio of row1 and row 2

I have a row called totalkills and one called totaldeaths, how could i have it return the ratio of them both EG totalkills = 100 totaldeaths = 10 ratio = 10/1

(not sure if php or sql forum is best!)

Thanks for your time

2. Is this a trick question or something? SELECT totalkills/totaldeaths FROM table

3. ...

and how does that helpget the ratio?

4. That is the query you use, and it will return the ratio you are looking for.

Dan

5. If you're aiming to get fractions from the table you can use this:

Code:
```DELIMITER ~~

CREATE FUNCTION normalized_fraction(a int, b int) RETURNS text
LANGUAGE SQL
DETERMINISTIC
NO SQL
BEGIN
DECLARE tmp, orig_a, orig_b int;

IF b = 0 THEN
RETURN NULL;
END IF;

SET orig_a = a;
SET orig_b = b;

REPEAT
SET tmp = b;
SET b = a % b;
SET a = tmp;
UNTIL b = 0
END REPEAT;

SET orig_a = orig_a / a;
SET orig_b = orig_b / a;
RETURN CONCAT(orig_a, '/', orig_b);
END~~```
After which:

Code:
```mysql> SELECT *, totalkills/totaldeaths AS ratio, normalized_fraction(totalkills, totaldeaths) AS fractional_ratio FROM test;
+------------+-------------+----------+------------------+
| totalkills | totaldeaths | ratio    | fractional_ratio |
+------------+-------------+----------+------------------+
|       1000 |          10 | 100.0000 | 100/1            |
|         10 |        1000 |   0.0100 | 1/100            |
|         10 |           0 |     NULL | NULL             |
|          0 |          10 |   0.0000 | 0/1              |
+------------+-------------+----------+------------------+
4 rows in set (0.00 sec)```

6. ok, (totalkills/totaldeaths) as totalkd works

How about doing the math when entering the data

I tried

PHP Code:
``` \$totalkd = trim(\$playersArray[\$i][2])/trim(\$playersArray[\$i][3]);  ```
and

PHP Code:
``` \$totalkd = \$totalkills/\$totaldeaths;  ```
And they both return single numbers

edit fixed, forgot to set it to float

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•