Found 4378 Articles for MySQL

How to add some days to str_to_date() MySQL function?

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

108 Views

You can use DATE_ADD() function from MySQL. Let us first create a table −mysql> create table DemoTable    -> (    -> ShippingDate varchar(100)    -> ); Query OK, 0 rows affected (0.67 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values('06-01-2019'); Query OK, 1 row affected (0.15 sec) mysql> insert into DemoTable values('01-04-2017'); Query OK, 1 row affected (0.19 sec) mysql> insert into DemoTable values('29-06-2019'); Query OK, 1 row affected (0.47 sec)Display all records from the table using select statement −mysql> select *from DemoTable;Output+--------------+ | ShippingDate | +--------------+ | 06-01-2019   | ... Read More

Set numbers as a varchar field and compare in MySQL

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

100 Views

Let us first create a table −mysql> create table DemoTable    -> (    -> StudentId int NOT NULL AUTO_INCREMENT PRIMARY KEY,    -> StudentScore varchar(100)    -> ); Query OK, 0 rows affected (0.66 sec)Insert some records in the table using insert command −mysql> insert into DemoTable(StudentScore) values('90'); Query OK, 1 row affected (0.21 sec) mysql> insert into DemoTable(StudentScore) values('100'); Query OK, 1 row affected (0.13 sec) mysql> insert into DemoTable(StudentScore) values('56'); Query OK, 1 row affected (0.10 sec) mysql> insert into DemoTable(StudentScore) values('98'); Query OK, 1 row affected (0.13 sec)Display all records from the table ... Read More

How to add a column using MySQL SELECT in an already created table?

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

221 Views

Let us first create a table −mysql> create table DemoTable    -> (    -> Id int NOT NULL AUTO_INCREMENT PRIMARY KEY,    -> Name varchar(100),    -> Age int    -> ); Query OK, 0 rows affected (0.49 sec)Insert some records in the table using insert command −mysql> insert into DemoTable(Name, Age) values('Robert', 24); Query OK, 1 row affected (0.18 sec) mysql> insert into DemoTable(Name, Age) values('Chris', 22); Query OK, 1 row affected (0.13 sec)Display all records from the table using select statement −mysql> select *from DemoTable;Output+----+--------+------+ | Id | Name | Age | +----+--------+------+ ... Read More

Display all records ignoring the current date record in MySQL

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

86 Views

Let us first create a table −mysql> create table DemoTable    -> (    -> Name varchar(100),    -> DueDate datetime    -> ); Query OK, 0 rows affected (0.54 sec)Insert some records in the table using insert command. Let’s say the current date is “2019-07-05” −mysql> insert into DemoTable values('Chris', '2019-06-24'); Query OK, 1 row affected (0.12 sec) mysql> insert into DemoTable values('Chris', '2018-01-01'); Query OK, 1 row affected (0.13 sec) mysql> insert into DemoTable values('Robert', '2019-07-05'); Query OK, 1 row affected (0.21 sec) mysql> insert into DemoTable values('Carol', '2019-08-03'); Query OK, 1 row affected (0.22 ... Read More

Concatenate date and time from separate columns into a single column in MySQL

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

208 Views

For this, concatenate both the date and time using CONCAT() function. Let us first create a table −mysql> create table DemoTable    -> (    -> ShippingDate date,    -> ShippingTime time,    -> ShippingDatetime datetime    -> ); Query OK, 0 rows affected (0.50 sec)Insert some records in the table using insert command −mysql> insert into DemoTable(ShippingDate, ShippingTime) values('2019-01-10', '10:40:20'); Query OK, 1 row affected (0.13 sec) mysql> insert into DemoTable(ShippingDate, ShippingTime) values('2019-06-14', '04:00:10'); Query OK, 1 row affected (0.14 sec)Display all records from the table using select statement −mysql> select *from DemoTable;Output+--------------+--------------+------------------+ | ShippingDate | ShippingTime | ... Read More

How to SELECT fields from one table and INSERT to another in MySQL?

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

151 Views

Let us first create a table −mysql> create table DemoTable1    -> (    -> StudentId int,    -> StudentName varchar(20)    -> ); Query OK, 0 rows affected (0.64 sec)Insert some records in the table using insert command −mysql> insert into DemoTable1 values(10, 'John'); Query OK, 1 row affected (0.15 sec) mysql> insert into DemoTable1 values(11, 'Chris'); Query OK, 1 row affected (0.15 sec)Display all records from the table using select statement −mysql> select *from DemoTable1;Output+-----------+-------------+ | StudentId | StudentName | +-----------+-------------+ | 10 | John ... Read More

MySQL query to get only the minutes from datetime?

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

207 Views

Let us first create a table −mysql> create table DemoTable    -> (    -> ShippingDate datetime    -> ); Query OK,  0 rows affected (0.52 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values('2019-01-10 10:04:45'); Query OK,  1 row affected (0.18 sec) mysql> insert into DemoTable values('2019-06-11 05:45:00'); Query OK,  1 row affected (0.23 sec) mysql> insert into DemoTable values('2019-06-12 07:00:55'); Query OK,  1 row affected (0.14 sec)Display all records from the table using select statement −mysql> select *from DemoTable;Output+---------------------+ | ShippingDate | +---------------------+ | 2019-01-10 10:04:45 | | 2019-06-11 05:45:00 | | 2019-06-12 07:00:55 | +---------------------+ 3 rows in set (0.00 sec)Here is the query to get minutes in MySQL −mysql> select minute(ShippingDate) as Minutes from DemoTable;Output+---------+ | Minutes | +---------+ | 4 ... Read More

MySQL query to select a count on two separate conditions?

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

183 Views

Use CASE statement for this. Let us first create a table −mysql> create table DemoTable    -> (    -> StudentMarks int,    -> isValid tinyint(1)    -> ); Query OK, 0 rows affected (0.68 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values(45, 0); Query OK, 1 row affected (0.15 sec) mysql> insert into DemoTable values(78, 1); Query OK, 1 row affected (0.26 sec) mysql> insert into DemoTable values(45, 1); Query OK, 1 row affected (0.31 sec) mysql> insert into DemoTable values(78, 1); Query OK, 1 row affected (0.22 sec) ... Read More

How to return rows that have the same column values in MySQL?

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

192 Views

Use GROUP BY clause for this. Let us first create a table −mysql> create table DemoTable    -> (    -> StudentId int,    -> StudentMarks int    -> ); Query OK, 0 rows affected (4.71 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values(23, 58); Query OK, 1 row affected (0.70 sec) mysql> insert into DemoTable values(25, 89); Query OK, 1 row affected (0.46 sec) mysql> insert into DemoTable values(26, 58); Query OK, 1 row affected (1.13 sec) mysql> insert into DemoTable values(28, 98); Query OK, 1 row affected (0.86 sec)Display ... Read More

Implement MySQL LIMIT and OFFSET in a single query stating its difference

karthikeya Boyini
Updated on 30-Jun-2020 09:56:32

615 Views

The LIMIT tells about how many records you want while OFFSET gives the records from the given position+1. Let us first create a table −mysql> create table DemoTable    -> (    -> Name varchar(100)    -> ); Query OK, 0 rows affected (1.33 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values('John'); Query OK, 1 row affected (0.13 sec) mysql> insert into DemoTable values('Chris'); Query OK, 1 row affected (0.27 sec) mysql> insert into DemoTable values('David'); Query OK, 1 row affected (0.27 sec) mysql> insert into DemoTable values('Bob'); Query OK, 1 ... Read More

Advertisements