Found 4378 Articles for MySQL

How to update a column with the same values (not all) using UPDATE, SET and LIMIT in MySQL?

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

215 Views

Let us first create a table −mysql> create table DemoTable    -> (    -> Id int NOT NULL AUTO_INCREMENT PRIMARY KEY,    -> Name varchar(20)    -> ); Query OK, 0 rows affected (0.69 sec)Insert some records in the table using insert command −mysql> insert into DemoTable(Name) values('Carol'); Query OK, 1 row affected (0.17 sec) mysql> insert into DemoTable(Name) values('Chris'); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable(Name) values('David'); Query OK, 1 row affected (0.18 sec) mysql> insert into DemoTable(Name) values('Bob'); Query OK, 1 row affected (0.15 sec) mysql> insert into DemoTable(Name) ... Read More

Is there PHP basename() equivalent in MySQL?

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

111 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)    -> ); Query OK, 0 rows affected (1.02 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values('C:\Web\Sum.java'); Query OK, 1 row affected (0.11 sec) mysql> insert into DemoTable values('E:\WebDevelopment\Image1.png'); Query OK, 1 row affected (0.42 sec)Display all records from the table using select statement ... Read More

MySQL query that returns a specific string if column is null?

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

123 Views

Let us first create a table −mysql> create table DemoTable    -> (    -> Name varchar(100)    -> ); Query OK, 0 rows affected (0.85 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values('John'); Query OK, 1 row affected (0.17 sec) mysql> insert into DemoTable values(NULL); Query OK, 1 row affected (0.16 sec) mysql> insert into DemoTable values(NULL); Query OK, 1 row affected (0.20 sec) mysql> insert into DemoTable values('David'); Query OK, 1 row affected (0.17 sec)Display all records from the table using select statement −mysql> select *from DemoTable;Output+-------+ | Name ... Read More

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

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

358 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 OK, 1 row affected (0.22 sec) mysql> insert into DemoTable values(false); Query OK, 1 row affected (0.21 sec) mysql> insert into DemoTable values(false); Query OK, 1 row affected (0.13 sec) mysql> insert into DemoTable values(true); Query OK, 1 row affected (0.19 sec)Display all records from the table ... Read More

How to sum time in MySQL by converting into seconds?

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

2K+ Views

To convert time to seconds, use the TIME_TO_SEC() method. Let us first create a table −mysql> create table DemoTable    -> (    -> ArrivalTime time    -> ); Query OK, 0 rows affected (0.63 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values('04:10:00'); Query OK, 1 row affected (0.15 sec) mysql> insert into DemoTable values('05:20:50'); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable values('06:40:10'); Query OK, 1 row affected (0.18 sec)Display all records from the table using select statement −mysql> select *from DemoTable;Output+-------------+ | ArrivalTime | +-------------+ | 04:10:00 ... Read More

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

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

157 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 int    -> ); Query OK, 0 rows affected (0.59 sec)We will now create the second table, let’s say after 5 minutes −mysql> create table DemoTable2    -> (    -> StudentId int NOT NULL AUTO_INCREMENT PRIMARY KEY,    -> StudentName varchar(100),    -> StudentAge int    -> ); Query ... Read More

How to find current size (in memory) of table in MySQL?

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

179 Views

To get the current size of a table, use the following that will display details about a table including the size −show table status like ‘yourTableName’\GLet us first create a table −mysql> create table DemoTable    -> (    -> Id int NOT NULL AUTO_INCREMENT PRIMARY KEY,    -> CustomerName varchar(20),    -> CustomerAge int,    -> CustomerCountryName varchar(20)    -> ); Query OK, 0 rows affected (0.75 sec)Insert some records in the table using insert command −mysql> insert into DemoTable(CustomerName, CustomerAge, CustomerCountryName) values('John', 24, 'US'); Query OK, 1 row affected (0.23 sec) mysql> insert into DemoTable(CustomerName, CustomerAge, CustomerCountryName) ... Read More

How to work with auto incrementing column in MySQL?

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

91 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),    -> LastName varchar(20)    -> ); Query OK,  0 rows affected (0.71 sec)Insert some records in the table using insert command −mysql> insert into DemoTable(FirstName, LastName) values('John', 'Smith'); Query OK,  1 row affected (0.14 sec) mysql> insert into DemoTable(FirstName, LastName) values('Chris', 'Brown'); Query OK,  1 row affected (0.13 sec) mysql> insert into DemoTable(FirstName, LastName) values('Carol', 'Taylor'); Query OK,  1 row affected (0.21 sec)Display all records from the table using select statement −mysql> select *from DemoTable;Output+----+-----------+----------+ | Id | FirstName | LastName | +----+-----------+----------+ | 1  | John  ... Read More

MySQL query to order and display difference between dates from the current date

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

157 Views

For this, use ORDER BY clause. The current date is as follows −mysql> select now(); +---------------------+ | now() | +---------------------+ | 2019-06-09 21:08:16 | +---------------------+ 1 row in set (0.00 sec)Let us first create a table −mysql> create table DemoTable    -> (    -> Id int NOT NULL AUTO_INCREMENT PRIMARY KEY,    -> DueDate datetime    -> ); Query OK, 0 rows affected (0.62 sec)Insert some records in the table using insert command −mysql> insert into DemoTable(DueDate) values('2019-06-12'); Query OK, 1 row affected (0.24 sec) ... Read More

MySQL query to update string field by concatenating to it?

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

522 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 insert command −mysql> insert into DemoTable(StudentId) values('STU'); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable(StudentId) values('STU1'); Query OK, 1 row affected (0.18 sec)Display all records from the table using select statement −mysql> select *from DemoTable;Output+------------+-----------+ | SequenceId | StudentId | +------------+-----------+ | 1 ... Read More

Advertisements