Select Rows Older Than Current Date in MySQL

AmitDiwan
Updated on 03-Sep-2019 14:41:53

877 Views

Let’s say the current date is 2019-08-03. Now, we will see an example and create a table −mysql> create table DemoTable840(DueDate datetime); Query OK, 0 rows affected (0.66 sec)Insert some records in the table using insert command −mysql> insert into DemoTable840 values('2019-08-9'); Query OK, 1 row affected (0.10 sec) mysql> insert into DemoTable840 values('2019-07-5'); Query OK, 1 row affected (0.16 sec) mysql> insert into DemoTable840 values('2019-08-10'); Query OK, 1 row affected (0.15 sec) mysql> insert into DemoTable840 values('2019-07-13'); Query OK, 1 row affected (0.10 sec)Display all records from the table using select statement −mysql> select *from DemoTable840;This will produce the ... Read More

MySQL Query to Sum Rows with Repeated Corresponding ID

AmitDiwan
Updated on 03-Sep-2019 14:39:00

268 Views

To sum, you can use aggregate function SUM(). Let us first create a table −mysql> create table DemoTable850(    Id int,    Price int ); Query OK, 0 rows affected (0.54 sec)Insert some records in the table using insert command −mysql> insert into DemoTable850 values(1, 90); Query OK, 1 row affected (0.15 sec) mysql> insert into DemoTable850 values(2, 100); Query OK, 1 row affected (0.55 sec) mysql> insert into DemoTable850 values(2, 50); Query OK, 1 row affected (0.11 sec) mysql> insert into DemoTable850 values(1, 80); Query OK, 1 row affected (0.13 sec) mysql> insert into DemoTable850 values(1, 60); Query OK, ... Read More

Change the CURDATE() Current Date Format in MySQL

AmitDiwan
Updated on 03-Sep-2019 14:23:43

9K+ Views

The current date format is ‘YYYY-mm-dd’. To change current date format, you can use date_format().Let us first display the current date −mysql> select curdate();This will produce the following output −+------------+ | curdate() | +------------+ | 2019-08-08 | +------------+ 1 row in set (0.00 sec)Following is the query to change curdate() (current date) format −mysql> select date_format(curdate(), '%m/%d/%Y');This will produce the following output −+------------------------------------+ | date_format(curdate(), '%m/%d/%Y') | +------------------------------------+ | 08/08/2019                         | +------------------------------------+ 1 row in set (0.00 sec)Let us first create a table −mysql> create table DemoTable ... Read More

Multiply Values of Two Rows and Add the Result in MySQL

AmitDiwan
Updated on 03-Sep-2019 14:19:33

1K+ Views

For this, use aggregate function SUM(). Within this method, multiply the row values. Let us first create a table −mysql> create table DemoTable(    ProductQuantity int,    ProductPrice int ); Query OK, 0 rows affected (0.48 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values(10, 9); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable values(6, 20); Query OK, 1 row affected (0.19 sec) mysql> insert into DemoTable values(7, 100); Query OK, 1 row affected (0.08 sec)Display all records from the table using select statement −mysql> select *from DemoTable;This will produce the following ... Read More

Get the Sum of Columns for Duplicate Records in MySQL

AmitDiwan
Updated on 03-Sep-2019 13:57:26

989 Views

For this, use GROUP BY clause along with aggregate function SUM(). Let us first create a table −mysql> create table DemoTable(    Name varchar(100),    Score int ); Query OK, 0 rows affected (0.70 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values('Adam', 50); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable values('Bob', 80); Query OK, 1 row affected (0.13 sec) mysql> insert into DemoTable values('Adam', 70); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable values('Adam', 10); Query OK, 1 row affected (0.12 sec) mysql> insert into DemoTable values('Carol', ... Read More

Display Selected Records from a MySQL Table with IN Operator

AmitDiwan
Updated on 03-Sep-2019 13:52:06

180 Views

Let us first create a table −mysql> create table DemoTable(    Id int NOT NULL AUTO_INCREMENT PRIMARY KEY,    CustomerName varchar(100) ); Query OK, 0 rows affected (0.51 sec)Insert some records in the table using insert command −mysql> insert into DemoTable(CustomerName) values('Adam Smith'); Query OK, 1 row affected (0.17 sec) mysql> insert into DemoTable(CustomerName) values('Chris Brown'); Query OK, 1 row affected (0.13 sec) mysql> insert into DemoTable(CustomerName) values('David Miller'); Query OK, 1 row affected (0.15 sec) mysql> insert into DemoTable(CustomerName) values('Carol Taylot'); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable(CustomerName) values('Carol Taylor'); Query OK, 1 row affected ... Read More

Sum of Positive and Negative Values in Separate Columns with MySQL

AmitDiwan
Updated on 03-Sep-2019 13:46:24

7K+ Views

For this, you can use CASE statement. Let us first create a table −mysql> create table DemoTable(    Id int,    Value int ); Query OK, 0 rows affected (0.51 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values(10, 100); Query OK, 1 row affected (0.15 sec) mysql> insert into DemoTable values(10, -110); Query OK, 1 row affected (0.10 sec) mysql> insert into DemoTable values(10, 200); Query OK, 1 row affected (0.10 sec) mysql> insert into DemoTable values(10, -678); Query OK, 1 row affected (0.17 sec)Display all records from the table using select statement −mysql> ... Read More

MySQL Query to Insert Row with Date

AmitDiwan
Updated on 03-Sep-2019 13:42:12

475 Views

Let us first create a table −mysql> create table DemoTable(    EmployeeId int NOT NULL AUTO_INCREMENT PRIMARY KEY,    EmployeeName varchar(100),    JoiningDate date ); Query OK, 0 rows affected (0.48 sec)Insert some records in the table using insert command −mysql> insert into DemoTable(EmployeeName, JoiningDate) values('Chris', '2019-01-21'); Query OK, 1 row affected (0.20 sec) mysql> insert into DemoTable(EmployeeName, JoiningDate) values('Robert', '2016-12-01'); Query OK, 1 row affected (0.15 sec) mysql> insert into DemoTable(EmployeeName, JoiningDate) values('Mike', '2015-03-12'); Query OK, 1 row affected (0.12 sec)Display all records from the table using select statement −mysql> select *from DemoTable;This will produce the following output −+------------+--------------+-------------+ ... Read More

Insert Data into New Column of Existing MySQL Table

AmitDiwan
Updated on 03-Sep-2019 13:38:51

5K+ 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.47 sec)Insert some records in the table using insert command −mysql> insert into DemoTable(Name) values('John'); Query OK, 1 row affected (0.10 sec) mysql> insert into DemoTable(Name) values('Bob'); Query OK, 1 row affected (0.10 sec) mysql> insert into DemoTable(Name) values('Adam'); Query OK, 1 row affected (0.28 sec)Display all records from the table using select statement −mysql> select *from DemoTable;This will produce the following output −+----+------+ | Id | Name | +----+------+ | 1 | ... Read More

What is SELECT TRUE in MySQL

AmitDiwan
Updated on 03-Sep-2019 13:16:09

3K+ Views

The statement SELECT TRUE returns 1 if a row match. Let us first create a table −mysql> create table DemoTable(Name varchar(100)); Query OK, 0 rows affected (0.54 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values('Chris'); Query OK, 1 row affected (0.15 sec) mysql> insert into DemoTable values('Robert'); Query OK, 1 row affected (0.16 sec) mysql> insert into DemoTable values('David'); Query OK, 1 row affected (0.12 sec)Display all records from the table using select statement −mysql> select *from DemoTable;This will produce the following output −+--------+ | Name   | +--------+ | Chris  | | Robert ... Read More

Advertisements