How can column data values of a table be compared using MySQL STRCMP() function?

MySQLMySQLi Database

If we want to compare the data values of two columns then we need to provide the name of the columns as arguments of MySQL STRCMP() function. Suppose we have a table named ‘Marks’ which contains the name of the student and their secured marks in different subjects. Now, if we want to know that a particular student has got more or less or equal marks in two subjects then it can be illustrated using STRCMP() function as follows −

Example

mysql> Select Name, STRCMP(Math,Hindi) from student marks WHERE Name = 'Rahul';
+-------+--------------------+
| Name  | STRCMP(Math,Hindi) |
+-------+--------------------+
| Rahul |                  1 |
+-------+--------------------+
1 row in set (0.00 sec)

The query above will compare the marks of Math and Hindi for a student named ‘Rahul’. It is clear from the result set that Rahul secure more marks in Math rather than in Hindi.

raja
Published on 20-Feb-2018 16:42:46
Advertisements