AmitDiwan has Published 11365 Articles

Select rows from a MySQL table and display using IN()

AmitDiwan

AmitDiwan

Updated on 09-Sep-2019 08:04:43

160 Views

Let us first create a table −mysql> create table DemoTable778 ( ClientId varchar(100), ClientName varchar(100) ); Query OK, 0 rows affected (1.05 sec)Insert some records in the table using insert command −mysql> insert into DemoTable778 values('J-101', 'John'); Query OK, 1 row affected (0.20 sec) ... Read More

MySQL query to display the column and its values using OR in WHERE statement

AmitDiwan

AmitDiwan

Updated on 09-Sep-2019 08:02:13

57 Views

Let us first create a table −mysql> create table DemoTable777 ( StudentId int NOT NULL AUTO_INCREMENT PRIMARY KEY, StudentName varchar(100), StudentAge int ); Query OK, 0 rows affected (1.34 sec)Insert some records in the table using insert command −mysql> insert into ... Read More

Why does the following error occur in MySQL: ERROR 1062 (23000): Duplicate entry?

AmitDiwan

AmitDiwan

Updated on 09-Sep-2019 07:59:17

3K+ Views

Let’s say you have set column values as unique key and try to insert duplicate values in the table. This will lead to ERROR 1062 (23000): Duplicate entry.Let us first create a table -mysql> create table DemoTable776 (    FirstValue int,    SecondValue int,    unique key(FirstValue, SecondValue) ); Query ... Read More

Selecting rows that are older than current date in MySQL?

AmitDiwan

AmitDiwan

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

623 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> ... Read More

MySQL query to sum rows having repeated corresponding Id

AmitDiwan

AmitDiwan

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

210 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 ... Read More

Change the curdate() (current date) format in MySQL

AmitDiwan

AmitDiwan

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

8K+ 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 ... Read More

MySQL query to multiply values of two rows and add the result

AmitDiwan

AmitDiwan

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

894 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, ... Read More

Get the sum of columns for duplicate records in MySQL

AmitDiwan

AmitDiwan

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

823 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 ... Read More

Display selected records from a MySQL table with IN() operator

AmitDiwan

AmitDiwan

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

119 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) ... Read More

Display the sum of positive and negative values from a column in separate columns with MySQL

AmitDiwan

AmitDiwan

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

6K+ 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 ... Read More

Advertisements