display GB value with IF() that maps 0 to a dash : IF « Function « SQL / MySQL






display GB value with IF() that maps 0 to a dash

      
mysql>
mysql> CREATE TABLE standings1
    -> (
    ->  team    CHAR(20),               # team name
    ->  wins    INT,                    # number of wins
    ->  losses  INT                             # number of losses
    -> );
Query OK, 0 rows affected (0.00 sec)

mysql>
mysql> # Load Northern League final 1902 standings into the table
mysql>
mysql> INSERT INTO standings1 (team, wins, losses) VALUES ('Winnipeg',37,20);
Query OK, 1 row affected (0.00 sec)

mysql> INSERT INTO standings1 (team, wins, losses) VALUES ('Crookston',31,25);
Query OK, 1 row affected (0.00 sec)

mysql> INSERT INTO standings1 (team, wins, losses) VALUES ('Fargo',30,26);
Query OK, 1 row affected (0.00 sec)

mysql> INSERT INTO standings1 (team, wins, losses) VALUES ('Grand Forks',28,26);
Query OK, 1 row affected (0.00 sec)

mysql> INSERT INTO standings1 (team, wins, losses) VALUES ('Devils Lake',19,31);
Query OK, 1 row affected (0.00 sec)

mysql> INSERT INTO standings1 (team, wins, losses) VALUES ('Cavalier',15,32);
Query OK, 1 row affected (0.00 sec)

mysql>
mysql> SELECT team, wins AS W, losses AS L,
    -> TRUNCATE(wins/(wins+losses),3) AS PCT,
    -> IF((@wl_diff - (wins-losses)) = 0,'-',(@wl_diff - (wins-losses))/2) AS GB
    -> FROM standings1
    -> ORDER BY wins-losses DESC, PCT DESC;
+-------------+------+------+-------+---------+
| team        | W    | L    | PCT   | GB      |
+-------------+------+------+-------+---------+
| Winnipeg    |   37 |   20 | 0.649 | -       |
| Crookston   |   31 |   25 | 0.553 | 5.5000  |
| Fargo       |   30 |   26 | 0.535 | 6.5000  |
| Grand Forks |   28 |   26 | 0.518 | 7.5000  |
| Devils Lake |   19 |   31 | 0.380 | 14.5000 |
| Cavalier    |   15 |   32 | 0.319 | 17.0000 |
+-------------+------+------+-------+---------+
6 rows in set (0.00 sec)

mysql>
mysql> drop table standings1;
Query OK, 0 rows affected (0.00 sec)

   
    
    
    
    
    
  








Related examples in the same category

1.The IF() function compares three expressions, as shown in the following syntax:
2.Use an IF function to evaluate which designs sell over 500 units during the winter sales quarter
3.SELECT IF('02-29' < '03-01','02-29','03-01') AS earliest;
4.IF(condition, result1, result2)
5.Mapping NULL Values to Other Values for Display