Rama Giri has Published 107 Articles

How can I set 0 if a query returns a null value in MySQL?

Rama Giri

Rama Giri

Updated on 30-Jul-2019 22:30:26

825 Views

For this, you can use IFNULL(). Let us first create a table −mysql> create table DemoTable    -> (    -> Id int NOT NULL AUTO_INCREMENT PRIMARY KEY,    -> Value int    -> ); Query OK, 0 rows affected (0.71 sec)Insert some records in the table using insert command ... Read More

How to sort multiple columns with a single query?

Rama Giri

Rama Giri

Updated on 30-Jul-2019 22:30:26

226 Views

Let us first create a table −mysql> create table DemoTable     -> (     -> Id int,     -> Value int     -> ); Query OK, 0 rows affected (0.68 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values(100, 85885); Query ... Read More

MySQL query to select records beginning from a specific id

Rama Giri

Rama Giri

Updated on 30-Jul-2019 22:30:26

2K+ Views

Let us first create a table −mysql> create table DemoTable    -> (    -> Id int NOT NULL AUTO_INCREMENT PRIMARY KEY,    -> Name varchar(100)    -> ); Query OK, 0 rows affected (0.67 sec)Insert some records in the table using insert command −mysql> insert into DemoTable(Name) values('John'); Query ... Read More

MySQL query to select rows older than a week?

Rama Giri

Rama Giri

Updated on 30-Jul-2019 22:30:26

319 Views

For this, you can use DATEDIFF() function. The current date time is as follows −mysql> select now(); +---------------------+ | now() | +---------------------+ | 2019-06-09 19:15:56 | +---------------------+ 1 row in set (0.00 sec)Let us first create ... Read More

How to set a comma separated list as a table in MySQL?

Rama Giri

Rama Giri

Updated on 30-Jul-2019 22:30:26

509 Views

You can use UNION ALL for this.Let us get list 10, 20, 30, 40, 50 as a table with UNION ALL −mysql> select 10 Number UNION ALL select 20 Number UNION ALL select 30 Number     UNION ALL select 40 Number UNION ALL select 50 Number;Output+--------+ | Number | +--------+ | 10 | | 20 | | 30 ... Read More

MySQL query to update string field by concatenating to it?

Rama Giri

Rama Giri

Updated on 30-Jul-2019 22:30:26

916 Views

For concatenating a string field, use CONCAT() function. Let us first create a table −mysql> create table DemoTable    -> (    -> SequenceId int NOT NULL AUTO_INCREMENT PRIMARY KEY,    -> StudentId varchar(100)    -> ); Query OK, 0 rows affected (0.59 sec)Insert some records in the table using ... Read More

How to work with auto incrementing column in MySQL?

Rama Giri

Rama Giri

Updated on 30-Jul-2019 22:30:26

121 Views

To work with auto incrementing column, you can set it as AUTO_INCREMENT while creating the table.Let us first create a table. Here, we have set the Id field as column since that would be our auto increment column −mysql> create table DemoTable    -> (    -> Id int NOT NULL AUTO_INCREMENT PRIMARY KEY,    -> FirstName varchar(20), ... Read More

How to get the creation time of recently created table in MySQL?

Rama Giri

Rama Giri

Updated on 30-Jul-2019 22:30:26

190 Views

Following is the syntax −select table_name, create_time from information_schema.TABLES where table_schema = 'yourDataBaseName' order by CREATE_TIME desc limit 1;Let us create the first table (Time: 2019-06-10 16:40:51) −mysql> create table DemoTable1    -> (    -> StudentId int NOT NULL AUTO_INCREMENT PRIMARY KEY,    -> StudentName varchar(100),    -> StudentAge ... Read More

Can we replace a number with a String in a MySQL result set?

Rama Giri

Rama Giri

Updated on 30-Jul-2019 22:30:26

475 Views

Yes, we can do that using the CASE statement. Let us first create a table −mysql> create table DemoTable    -> (    -> isMarried boolean    -> ); Query OK, 0 rows affected (0.76 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values(true); Query ... Read More

Is there PHP basename() equivalent in MySQL?

Rama Giri

Rama Giri

Updated on 30-Jul-2019 22:30:26

177 Views

If given a string containing a path to a file, the PHP basename() function will return the base name of the file. To get its equivalent in MySQL, you can use SUBSTRING_INDEX(). Let us first create a table −mysql> create table DemoTable    -> (    -> Location varchar(200)   ... Read More

Advertisements