klox
klox

Reputation: 2093

performing calculation in mysql

dear all..i have some data at DB.

name        Range
bla     123x9901-123x0000      //it means range 9901 until 10000 = 100
bla     123v0001-123v0100      // 10001-10100 = 100

i want the result like:

   name         Qty  
   bla          200

i counting them use:

SELECT................
      IF(RIGHT(Range,4) = "0000",10000,RIGHT(Range,4)) - MID(Range,5,4) + 1 AS Qty
FROM mytable GROUP BY name;

but the result:

name      Qty
bla       100

Upvotes: 1

Views: 124

Answers (1)

OMG Ponies
OMG Ponies

Reputation: 332581

Use:

  SELECT name,
         SUM(IF(RIGHT(Range,4) = "0000",10000, RIGHT(Range,4)) - MID(Range,5,4) + 1) AS Qty
    FROM mytable
GROUP BY name;

...gives me:

name    Qty  
-------------
bla     200

Upvotes: 1

Related Questions