Alternative for CONCAT in MySQL

AmitDiwan
Updated on 02-Jul-2020 06:55:49

983 Views

Yes, an alternative is CONCAT_WS(). Let us first create a table −mysql> create table DemoTable    (    StudentId int NOT NULL AUTO_INCREMENT PRIMARY KEY,    StudentName varchar(100)    ); Query OK, 0 rows affected (0.74 sec)ExampleInsert some records in the table using insert command −mysql> insert into DemoTable(StudentName) values('Chris'); Query OK, 1 row affected (0.13 sec) mysql> insert into DemoTable(StudentName) values('Robert'); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable(StudentName) values('Bob'); Query OK, 1 row affected (0.12 sec)Display all records from the table using select statement −mysql> select *from DemoTable;Output+-----------+-------------+ | StudentId | StudentName | +-----------+-------------+ | ... Read More

Split Comma-Delimited String into Array in PHP

AmitDiwan
Updated on 02-Jul-2020 06:54:26

2K+ Views

To split a given comma delimited string into an array of values, the PHP code is as follows −Example Live DemoOutputThe array is Array (    [0] => 456    [1] => 789    [2] => 23    [3] => 4    [4] => 019 ) The array is Array (    [0] => 00    [1] => 876    [2] => 5432    [3] => 1234    [4] => 0 )A string of numbers is defined and the ‘preg_split’ function is used to separate the numbers based on the occurance of ‘/’ or ‘.’. The split array is now printed. Another method to split the given number in the form of a string is to use the explode function. It splits the array based on the occurance of a comma.

Remove Non-Alphanumeric Characters from String in PHP

AmitDiwan
Updated on 02-Jul-2020 06:53:14

1K+ Views

To remove non-alphanumeric characters from string, the code is as follows −Example Live DemoOutputThe non-alphanumeric characters removed gives the string as ThisissampleonlyThe function ‘preg_replace’ is used to remove alphanumeric characters from the string. A regular expression is used to filter out the alphanumeric characters. The string is previously defined and the function ‘preg_replace’ is called on this and the reformed string is displayed on the console.Example Live DemoOutputThe non-alphanumeric characters removed gives the string as Thisisasample_onlyThe only difference here is that a different regular expression is used. It means the same as the previous regular expression, but written in a different fashion.Read More

Select Query to Display Duplicate Values with Max Date

AmitDiwan
Updated on 02-Jul-2020 06:53:00

1K+ Views

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

Compute Execution Time of a PHP Script

AmitDiwan
Updated on 02-Jul-2020 06:51:46

696 Views

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.

Implement Incremental Search and Display Values in MySQL

AmitDiwan
Updated on 02-Jul-2020 06:51:42

171 Views

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

Implement Interval Condition for Due Dates in MySQL

AmitDiwan
Updated on 02-Jul-2020 06:50:50

133 Views

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

Convert Timestamp into Time Ago in PHP

AmitDiwan
Updated on 02-Jul-2020 06:50:46

964 Views

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

Get Fixed Number of Results in Descending Order Using MySQL Query

AmitDiwan
Updated on 02-Jul-2020 06:49:41

225 Views

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

Calculate Total Time from Array of Times in PHP

AmitDiwan
Updated on 02-Jul-2020 06:48:50

2K+ Views

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

Advertisements