For this, use GROUP BY and HAVING. Let us first create a table −mysql> create table DemoTable ( StudentName varchar(100), DueDate date ); Query OK, 0 rows affected (0.72 sec)ExampleInsert some records in the table using insert command −mysql> insert into DemoTable values('John', '2019-01-11'); Query OK, 1 row affected (0.26 sec) mysql> insert into DemoTable values('Chris', '2019-02-11'); Query OK, 1 row affected (0.20 sec) mysql> insert into DemoTable values('Chris', '2019-03-11'); Query OK, 1 row affected (0.19 sec) mysql> insert into DemoTable values('John', '2019-04-11'); Query OK, 1 row affected (0.12 sec) mysql> insert into DemoTable values('Bob', '2019-05-11'); ... Read More
To compute the execution time of a PHP script, the code is as follows −Example Live DemoOutputThe execution time of the PHP script is : 1.69 secThe ‘microtime’ function can be used to check the time taken by a PHP script to execute completely. When the code begins execution, the time is recorded, and once the code is completed, another timestamp is generated and the difference between the end and star time is the time taken by the script to complete its execution.
For this, you can use SUBSTRING_INDEX(). Let us first create a table −mysql> create table DemoTable (Number varchar(100)); Query OK, 0 rows affected (0.60 sec)ExampleInsert some records in the table using insert command −mysql> insert into DemoTable values('235678'); Query OK, 1 row affected (0.20 sec) mysql> insert into DemoTable values('1634990'); Query OK, 1 row affected (0.23 sec) mysql> insert into DemoTable values('678590'); Query OK, 1 row affected (0.46 sec) mysql> insert into DemoTable values('908765432'); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable values('222343388773'); Query OK, 1 row affected (0.35 sec) mysql> insert into DemoTable values('09028215'); Query OK, ... Read More
Let us first create a table −mysql> create table DemoTable (DueDate date); Query OK, 0 rows affected (0.92 sec)ExampleInsert some records in the table using insert command −mysql> insert into DemoTable values('2019-01-12'); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable values('2019-04-01'); Query OK, 1 row affected (0.17 sec) mysql> insert into DemoTable values('2019-07-19'); Query OK, 1 row affected (0.15 sec)Display all records from the table using select statement −mysql> select *from DemoTable;Output+------------+ | DueDate | +------------+ | 2019-01-12 | | 2019-04-01 | | 2019-07-19 | +------------+ 3 rows in set (0.00 sec)Following is the query to ... Read More
To convert a given timestamp into time ago, the code is as follows −Example Live DemoOutputThe timestamp to time ago conversion is 10 minutes agoA function named ‘to_time_ago’ is defined that checks the difference between the time passed as parameter to the function and the time function. If this difference is found to be less than 1, it returns that the time passed just a second ago. Otherwise, the year, month, day, hour, minute, and second is generated in an array. A ‘foreach’ loop is used to iterate over the array previously generated. The time difference is computed and printed on ... Read More
For descending order result, use DESC. However, LIMIT is used to get fixed number of records −select *from yourTableName order by yourColumnName DESC LIMIT yourLimitNumber;Let us first create a table −mysql> create table DemoTable (Id int, Name varchar(100)); Query OK, 0 rows affected (0.73 sec)ExampleInsert some records in the table using insert command −mysql> insert into DemoTable values(100, 'John'); Query OK, 1 row affected (0.11 sec) mysql> insert into DemoTable values(101, 'Chris'); Query OK, 1 row affected (0.18 sec) mysql> insert into DemoTable values(102, 'Robert'); Query OK, 1 row affected (0.23 sec) mysql> insert into DemoTable values(103, 'Bob'); Query OK, ... Read More
The ‘strtotime’ function can be used to convert the given string into a time format. Let us see an example −Example Live DemoOutputThe total time is :-441915:-12:-58An array that contains time format data is defined and the ‘strtotime´function is used to convert the string into a time format. The ‘foreach’ loop is used to iterate over the elements in the time format array and the elements are added.The hours is calculated by dividing the value computed by 3600. The minutes is calculated by dividing the value computed by product of hours calculated and 3600. The seconds is calculated by dividing the ... Read More
Using bin2hex functionExample Live DemoOutputThe randomly generated string is : f1db16115fa93b98493d388bA number is defined and the bin2hex function is called on this number. Inside the bin2hex function the ‘random_bytes’ function is called on this number. The generated random string is printed on the console.Using hashing functionExample Live DemoOutputThe randomly generated string using hashing function sha1 is :9a4a73c35ac034832332977f3d5accd8eace5260A number is defined by calling the ‘rand’ function. The sha1 hashing function is called on this randomly generated number. The generated random string is printed on the console.Using built-in function uniqidExample Live DemoOutputThe randomly generated string using uniqid function is : 5ed4b884cef34A number is defined by ... Read More
To generate a numeric one-time password in PHP, the code is as follows −Example Live DemoOutputThe one time password generated is :52471609A function named ‘generate_otp’ is defined that takes the length as a parameter. This is the length of the password that needs to be generated. A number that contains 0 to 9 numbers is defined and the length is iterated over and a random number that contains these 0 to 9 numbers randomly is generated. The length is defined and the function is called on this length. This generates a numeric password and displays it on the console.
To round seconds to nearest half minute, use CEILING(). Let us first create a table −mysql> create table DemoTable (secondValue int); Query OK, 0 rows affected (0.64 sec)ExampleInsert some records in the table using insert command −mysql> insert into DemoTable values(27); Query OK, 1 row affected (0.24 sec) mysql> insert into DemoTable values(56); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable values(118); Query OK, 1 row affected (0.20 sec)Display all records from the table using select statement −mysql> select *from DemoTable;Output+-------------+ | secondValue | +-------------+ | 27 | | 56 ... Read More