Examples of Different Types of Operation

This section provides tutorial examples of different types of operations: numeric, predicate, date and time.

Example 1 - NumericOps.sql

-- NumericOps.sql
-- Copyright (c) 2015, HerongYang.com, All Rights Reserved.
--
SELECT 1 + 2 AS LINE_1;
SELECT 2 / 3 AS LINE_2;
SELECT 2.0000 / 3 AS LINE_3;
SELECT 1234567890/10000 AS LINE_4;
SELECT 1234567890/1000000000 AS LINE_5;
SELECT 1234567890/1e10 AS LINE_6;
SELECT 1234567890/1e13 AS LINE_7;
SELECT 1234567890/1e14 AS LINE_8;
SELECT 1 / 0 AS LINE_9;
SELECT 1.2345 - 0.2345 AS LINE_10;
SELECT 2.00 / 1 AS LINE_11;
SELECT 2.00 - 1 AS LINE_12;

Run NumericOps.sql on MySQL 5.7 server, you will get:

C:\herong>set "MySQL=\Program Files\MySQL\MySQL Server 5.7"
C:\herong>"%MySQL%\bin\mysql" --user=root --password=TopSecret test 
   < NumericOps.sql

LINE_1
3
LINE_2
0.6667
LINE_3
0.66666667
LINE_4
123456.7890
LINE_5
1.2346
LINE_6
0.123456789
LINE_7
0.000123456789
LINE_8
0.0000123456789
LINE_9
NULL
LINE_10
1.0000
LINE_11
2.000000
LINE_12
1.00

Note that:

Run NumericOps.sql on MySQL 5.6 and 5.0 servers, you will get the same result:

But run NumericOps.sql on MySQL 4.0 server, you will get:

LINE_1
3
LINE_2
0.67
LINE_3
0.666667
LINE_4
123456.79
LINE_5
1.23
LINE_6
0.123456789
LINE_7
0.000123456789
LINE_8
1.23456789e-005
LINE_9
NULL
LINE_10
1.0000
LINE_11
2.0000
LINE_12
1.00

As you can see, older version of MySQL are not as accurate as newer versions.

Example 2 - PredicateOps.sql

-- PredicateOps.sql
-- Copyright (c) 2015, HerongYang.com, All Rights Reserved.
--
SELECT 1 = 1 AS LINE_1;
SELECT 'abc' = 'ABC' AS LINE_2;
SELECT 'abc' < 'xyz' AS LINE_3;
SELECT 'abc' < 'abcd' AS LINE_4;
SELECT 0.67 = 2/3 AS LINE_5;
SELECT 2/3 BETWEEN 0.66 AND 0.67 AS LINE_6;
SELECT 'APPLE' IN ('apple', 'orange', 'banana') AS LINE_7;
SELECT 'abc' LIKE 'Abc' AS LINE_8;
SELECT '3.14' LIKE '_.__' AS LINE_9;
SELECT '3.14159' LIKE '3.%' AS LINE_10;
SELECT 'herong_yang' LIKE 'herong\_yang' AS LINE_11;
SELECT '4.75%' LIKE '_.__\%' AS LINE_12;

Run PredicateOps.sql on MySQL 5.7 server, you will get:

LINE_1
1
LINE_2
1
LINE_3
1
LINE_4
1
LINE_5
0
LINE_6
1
LINE_7
1
LINE_8
1
LINE_9
1
LINE_10
1
LINE_11
1
LINE_12
1

Note that:

Example 3 - DateTimeOps.sql

-- DateTimeOps.sql
-- Copyright (c) 2015, HerongYang.com, All Rights Reserved.
--
SELECT DATE '1999-01-01' + INTERVAL '3' MONTH AS LINE_1;
SELECT DATE '2000-03-01' - INTERVAL '1' DAY AS LINE_2;
SELECT INTERVAL '48' HOUR + TIMESTAMP '1999-01-01 00:00:00' AS LINE_3;

Run DateTimeOps.sql on MySQL 5.7 server, you will get:

LINE_1
1999-04-01
LINE_2
2000-02-29
LINE_3
1999-01-03 00:00:00

Last update: 2015.

Table of Contents

 About This Book

 Introduction of SQL

 MySQL Introduction and Installation

 Introduction of MySQL Programs

 Perl Programs and MySQL Servers

 PHP Programs and MySQL Servers

 Java Programs and MySQL Servers

 Datatypes and Data Literals

Operations and Expressions

 What Is Expression

 Arithmetic Operations

 Predicate Operations

 Date and Time Operations

Examples of Different Types of Operation

 Character Strings and Bit Strings

 Commonly Used Functions

 Table Column Types for Different Types of Values

 Using DDL to Create Tables and Indexes

 Using DML to Insert, Update and Delete Records

 Using SELECT to Query Database

 Transaction Management and Isolation Levels

 Locks Used in MySQL

 Defining and Calling Stored Procedures

 Variables, Loops and Cursors Used in Stored Procedures

 Outdated Tutorials

 References

 PDF Printing Version