Found 4378 Articles for MySQL

Get the sum of columns for duplicate records in MySQL

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

834 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

120 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

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

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 (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

390 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

Inserting data into a new column of an already existing table in MySQL?

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

4K+ 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

2K+ 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

How to display coming Sunday's date for all the date records in MySQL?

AmitDiwan
Updated on 03-Sep-2019 13:13:44

188 Views

Let us first create a table −mysql> create table DemoTable(GetSundayDate date); Query OK, 0 rows affected (0.44 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values('2019-08-07'); Query OK, 1 row affected (0.19 sec) mysql> insert into DemoTable values('2018-09-05'); Query OK, 1 row affected (0.22 sec) mysql> insert into DemoTable values('2019-09-12'); Query OK, 1 row affected (0.17 sec)Display all records from the table using select statement −mysql> select *from DemoTable;This will produce the following output −+---------------+ | GetSundayDate | +---------------+ | 2019-08-07    | | 2018-09-05    | | 2019-09-12    | +---------------+ 3 rows in ... Read More

Convert varchar to unsigned integer in MySQL

AmitDiwan
Updated on 03-Sep-2019 13:11:43

626 Views

To convert varchar to unsigned integer, use CAST() function. Let us first create a table −mysql> create table DemoTable868(Amount varchar(100)); Query OK, 0 rows affected (0.51 sec)Insert some records in the table using insert command −mysql> insert into DemoTable868 values('781'); Query OK, 1 row affected (0.18 sec) mysql> insert into DemoTable868 values('990'); Query OK, 1 row affected (0.25 sec) mysql> insert into DemoTable868 values('1002'); Query OK, 1 row affected (0.17 sec) mysql> insert into DemoTable868 values('560'); Query OK, 1 row affected (0.12 sec) mysql> insert into DemoTable868 values('890'); Query OK, 1 row affected (0.26 sec) mysql> insert into DemoTable868 values('9890'); ... Read More

How to get the top 3 salaries from a MySQL table with record of Employee Salaries?

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

1K+ Views

For this, use LIMIT and OFFSET. Let us first create a table −mysql> create table DemoTable867(EmployeeSalary int); Query OK, 0 rows affected (0.64 sec)Insert some records in the table using insert command −mysql> insert into DemoTable867 values(63737); Query OK, 1 row affected (0.12 sec) mysql> insert into DemoTable867 values(899833); Query OK, 1 row affected (0.10 sec) mysql> insert into DemoTable867 values(23644); Query OK, 1 row affected (0.20 sec) mysql> insert into DemoTable867 values(89393); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable867 values(534333); Query OK, 1 row affected (0.13 sec) mysql> insert into DemoTable867 values(889322); Query OK, 1 ... Read More

Perform SUM and SUBTRACTION on the basis of a condition in a single MySQL query?

AmitDiwan
Updated on 03-Sep-2019 13:05:37

2K+ Views

For this, use CASE statement and set for both SUM and SUBTRACTION. Let us first create a table −mysql> create table DemoTable866(    Status varchar(100),    Amount int ); Query OK, 0 rows affected (0.63 sec)Insert some records in the table using insert command −mysql> insert into DemoTable866 values('ACTIVE', 50); Query OK, 1 row affected (0.10 sec) mysql> insert into DemoTable866 values('INACTIVE', 70); Query OK, 1 row affected (0.12 sec) mysql> insert into DemoTable866 values('INACTIVE', 20); Query OK, 1 row affected (0.15 sec) mysql> insert into DemoTable866 values('ACTIVE', 100); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable866 ... Read More

Advertisements