Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6
  1. #1
    New Coder
    Join Date
    Dec 2004
    Posts
    33
    Thanks
    0
    Thanked 0 Times in 0 Posts

    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
    Regular Coder ralph l mayo's Avatar
    Join Date
    Nov 2005
    Posts
    951
    Thanks
    1
    Thanked 31 Times in 29 Posts
    Is this a trick question or something? SELECT totalkills/totaldeaths FROM table

  • #3
    New Coder
    Join Date
    Dec 2004
    Posts
    33
    Thanks
    0
    Thanked 0 Times in 0 Posts
    ...

    and how does that helpget the ratio?

  • #4
    Senior Coder whizard's Avatar
    Join Date
    Jan 2005
    Location
    Philadelphia, PA, USA
    Posts
    1,662
    Thanks
    14
    Thanked 76 Times in 76 Posts
    That is the query you use, and it will return the ratio you are looking for.

    Dan
    PHP Tip: If you want to use short tags (<? or <?=$var) then make sure short_open_tag is set to "1". It really helps.

    Don't forget to save everyone time and mark your thread as Resolved :)

    "Also note that it is your responsibility to die() if necessary."

    DON'T USE THE MYSQL_ EXTENSION

  • #5
    Regular Coder ralph l mayo's Avatar
    Join Date
    Nov 2005
    Posts
    951
    Thanks
    1
    Thanked 31 Times in 29 Posts
    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)
    Last edited by ralph l mayo; 07-04-2007 at 02:43 AM. Reason: Catch to make b == 0 a little less catastrophic

  • #6
    New Coder
    Join Date
    Dec 2004
    Posts
    33
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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
    Last edited by I3arry; 07-04-2007 at 07:17 PM.


  •  

    Posting Permissions

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