ABS()
function returns the absolute value of a number.
mysql> SELECT ABS(0); +--------+ | ABS(0) | +--------+ | 0 | +--------+ 1 row in set (0.00 sec) mysql> SELECT ABS(-199); +-----------+ | ABS(-199) | +-----------+ | 199 | +-----------+ 1 row in set (0.01 sec) mysql> SELECT ABS(199); +----------+ | ABS(199) | +----------+ | 199 | +----------+ 1 row in set (0.00 sec)
Let me
give an example. I am going to use following sample data.
CREATE
TABLE blog(
postId int PRIMARY KEY,
postTitle VARCHAR(25),
duration DECIMAL(6, 2)
);
CREATE
TABLE post_info(
postId int PRIMARY KEY,
likes int,
doslikes int,
shares int
);
INSERT
INTO blog VALUES(1, "Introduction To Java", 23.56);
INSERT
INTO blog VALUES(2, "Hello World to Haskell", 8.54);
INSERT
INTO blog VALUES(3, "SQL Queries", 104.43);
INSERT
INTO blog VALUES(4, "Static Vs Dynamic", 65.43);
INSERT
INTO blog VALUES(5, "IJSON", 65.09);
INSERT
INTO post_info VALUES(1, 123, 32, 10);
INSERT
INTO post_info VALUES(2, 654, 59, 8);
INSERT
INTO post_info VALUES(3, 76, 132, 80);
INSERT
INTO post_info VALUES(4, 987, 9, 100);
INSERT
INTO post_info VALUES(5, 87, 65, 5);
mysql> SELECT * FROM blog; +--------+------------------------+----------+ | postId | postTitle | duration | +--------+------------------------+----------+ | 1 | Introduction To Java | 23.56 | | 2 | Hello World to Haskell | 8.54 | | 3 | SQL Queries | 104.43 | | 4 | Static Vs Dynamic | 65.43 | | 5 | IJSON | 65.09 | +--------+------------------------+----------+ 5 rows in set (0.00 sec) mysql> mysql> SELECT * FROM post_info; +--------+-------+----------+--------+ | postId | likes | doslikes | shares | +--------+-------+----------+--------+ | 1 | 123 | 32 | 10 | | 2 | 654 | 59 | 8 | | 3 | 76 | 132 | 80 | | 4 | 987 | 9 | 100 | | 5 | 87 | 65 | 5 | +--------+-------+----------+--------+ 5 rows in set (0.00 sec)
Get
absolute difference between every post, which exist in table blog.
SELECT blg.postTitle,
ABS(post.likes-post.doslikes) AS "Difference between likes and
dislikes"
FROM blog AS blg
JOIN post_info AS post
ON blg.postId = post.postId;
No comments:
Post a Comment