Add a Year and Two Days to a Date with a Single MySQL Query

AmitDiwan
Updated on 11-Nov-2019 09:33:11

175 Views

For this, use INTERVAL in MySQL. Let us first create a table −mysql> create table DemoTable1376    -> (    -> AdmissionDate date    -> ); Query OK, 0 rows affected (0.68 sec)Insert some records in the table using insert command −mysql> insert into DemoTable1376 values('2018-01-21'); Query OK, 1 row affected (0.12 sec) mysql> insert into DemoTable1376 values('2017-12-01'); Query OK, 1 row affected (0.17 sec) mysql> insert into DemoTable1376 values('2018-11-02'); Query OK, 1 row affected (0.13 sec) mysql> insert into DemoTable1376 values('2019-03-14'); Query OK, 1 row affected (0.17 sec)Display all records from the table using select statement−mysql> select * from ... Read More

MySQL EXPLAIN Keyword: Execute or Explain Query

AmitDiwan
Updated on 11-Nov-2019 09:31:40

137 Views

The EXPLAIN keyword tells how MySQL executes the query. Let us first create a table −mysql> create table DemoTable1375    -> (    -> Id int NOT NULL AUTO_INCREMENT PRIMARY KEY,    -> FirstName varchar(20),    -> INDEX FIRST_INDEX(FirstName)    -> ); Query OK, 0 rows affected (0.73 sec)Insert some records in the table using insert command −mysql> insert into DemoTable1375(FirstName) values('Chris'); Query OK, 1 row affected (0.25 sec) mysql> insert into DemoTable1375(FirstName) values('Bob'); Query OK, 1 row affected (0.09 sec) mysql> insert into DemoTable1375(FirstName) values('Sam'); Query OK, 1 row affected (1.06 sec) mysql> insert into DemoTable1375(FirstName) values('David'); Query OK, ... Read More

Use COUNT with CASE Condition in MySQL Query

AmitDiwan
Updated on 11-Nov-2019 09:27:50

3K+ Views

Use CASE WHEN for this in MySQL and set CASE condition inside the COUNT() method to count. Let us first create a table −mysql> create table DemoTable1374    -> (    -> Id int NOT NULL AUTO_INCREMENT PRIMARY KEY,    -> Name varchar(20),    -> Score int    -> ); Query OK, 0 rows affected (0.61 sec)Insert some records in the table using insert command −mysql> insert into DemoTable1374(Name, Score) values('Chris', 45); Query OK, 1 row affected (0.13 sec) mysql> insert into DemoTable1374(Name, Score) values('David', 78); Query OK, 1 row affected (0.10 sec) mysql> insert into DemoTable1374(Name, Score) values('Bob', 45); ... Read More

Update MySQL Column with JSON Format

AmitDiwan
Updated on 11-Nov-2019 09:25:42

1K+ Views

To display records like JSON format, use MySQL concat(). Let us first create a table −mysql> create table DemoTable1373    -> (    -> StudentId int NOT NULL AUTO_INCREMENT PRIMARY KEY,    -> StudentDetails text    -> ); Query OK, 0 rows affected (0.86 sec)Insert some records in the table using insert command. Here, we haven’t inserted anything −mysql> insert into DemoTable1373 values(); Query OK, 1 row affected (0.27 sec) mysql> insert into DemoTable1373 values(); Query OK, 1 row affected (0.08 sec) mysql> insert into DemoTable1373 values(); Query OK, 1 row affected (0.18 sec)Display all records from the table using ... Read More

Subtracting a Number from a Single MySQL Column Value

AmitDiwan
Updated on 11-Nov-2019 09:23:28

777 Views

For this, just update the table and subtract. Let us first create a table −mysql> create table DemoTable1372    -> (    -> Value int    -> ); Query OK, 0 rows affected (0.77 sec)Insert some records in the table using insert command −mysql> insert into DemoTable1372 values(500); Query OK, 1 row affected (0.13 sec) mysql> insert into DemoTable1372 values(100); Query OK, 1 row affected (0.24 sec) mysql> insert into DemoTable1372 values(900); Query OK, 1 row affected (0.16 sec) mysql> insert into DemoTable1372 values(1000); Query OK, 1 row affected (0.32 sec)Display all records from the table using select statement −mysql> ... Read More

Get Values from All Rows and Display in a Single Row Separated by Comma with MySQL

AmitDiwan
Updated on 11-Nov-2019 09:21:18

2K+ Views

For this, use GROUP_CONCAT(). Do not use GROUP BY clause, since GROUP_CONTACT() is a better and quick solution.Let us first create a table −mysql> create table DemoTable1371    -> (    -> Id int,    -> CountryName varchar(40)    -> ); Query OK, 0 rows affected (0.89 sec)Insert some records in the table using insert command −mysql> insert into DemoTable1371 values(100, 'US'); Query OK, 1 row affected (0.15 sec) mysql> insert into DemoTable1371 values(100, 'UK'); Query OK, 1 row affected (0.21 sec) mysql> insert into DemoTable1371 values(101, 'AUS'); Query OK, 1 row affected (0.27 sec) mysql> insert into DemoTable1371 values(101, ... Read More

Get the Sum Only from Specific Cells in MySQL

AmitDiwan
Updated on 11-Nov-2019 09:19:59

147 Views

For only specific cells, set a condition with WHERE and use aggregate function SUM() to add. Let us first create a table −mysql> create table DemoTable1370    -> (    -> StudentName varchar(20),    -> Marks int    -> ); Query OK, 0 rows affected (0.87 sec)Insert some records in the table using insert command −mysql> insert into DemoTable1370 values('Adam Smith', 56); Query OK, 1 row affected (0.20 sec) mysql> insert into DemoTable1370 values('Chris Brown', 67); Query OK, 1 row affected (0.17 sec) mysql> insert into DemoTable1370 values('Adam Smith', 69); Query OK, 1 row affected (0.20 sec) mysql> insert into ... Read More

Learn English Easily in 2020: Everything You Should Know

Swetha Prasanna
Updated on 11-Nov-2019 07:58:05

508 Views

English is the most common language that we ever come across in our daily lives; be it in media, entertainment, education, profession, travel, and other aspects. It means with good knowledge of English; we can lead our lives better and achieve much more than we do without it.Many of us who fear English would have included learning English in our new year resolutions many times. But, we often neglect to learn English as time passes due to plenty of reasons −Lack of timeFear of being laughed at for speaking wrong EnglishUnaware of the importance of EnglishWell, as career guidance experts ... Read More

DateTimeOffset.AddMonths Method in C#

AmitDiwan
Updated on 11-Nov-2019 07:48:50

356 Views

The DateTimeOffset.AddMonths() method in C# is used to add a specified number of months to the value of this instance.SyntaxFollowing is the syntax −public DateTimeOffset AddMonths (int val);Above, Val is the number of months to be added. For subtracting months, set a negative value.ExampleLet us now see an example to implement the DateTimeOffset.AddMonths() method −using System; public class Demo {    public static void Main() {       DateTimeOffset dateTimeOffset = new DateTimeOffset(2019, 08, 10, 4, 20, 10, new TimeSpan(-5, 0, 0));       Console.WriteLine("DateTimeOffset (before adding months) = {0}", dateTimeOffset);       DateTimeOffset res = dateTimeOffset.AddMonths(3); ... Read More

Add Minutes to DateTimeOffset in C#

AmitDiwan
Updated on 11-Nov-2019 07:47:39

414 Views

The DateTimeOffset.AddMinutes() method in C# is used to adds a specified number of whole and fractional minutes to the value of this instance.SyntaxFollowing is the syntax −public DateTimeOffset AddMinutes (double val);Above, Val is the number of minutes to be added. To subtract, set a negative value for minutes.ExampleLet us now see an example to implement the DateTimeOffset.AddMinutes() method −using System; public class Demo {    public static void Main() {       DateTimeOffset dateTimeOffset = new DateTimeOffset(2019, 08, 10, 4, 20, 10, new TimeSpan(-5, 0, 0));       Console.WriteLine("DateTimeOffset (before adding minutes) = {0}", dateTimeOffset);       ... Read More

Advertisements