Found 4378 Articles for MySQL

Find sum with MySQL SUM() and give aliases for column heading

AmitDiwan
Updated on 23-Dec-2019 12:10:58

427 Views

For alias, use the following syntax wherein we are display an alias name −select sum(yourColumnName) as anyAliasName from yourTableName;Let us first create a table −mysql> create table DemoTable1800      (      Salary int      ); Query OK, 0 rows affected (0.00 sec)Insert some records in the table using insert command −mysql> insert into DemoTable1800 values(18000); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1800 values(32000); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1800 values(50000); Query OK, 1 row affected (0.00 sec)Display all records from the table using select statement −mysql> select * ... Read More

How to quote values of single column using GROUP_CONCAT and CONCAT with DISTINCT in MySQL?

AmitDiwan
Updated on 23-Dec-2019 12:09:34

348 Views

For this, you can use group_concat() along with replace(). Let us first create a table −mysql> create table DemoTable1799      (      EmployeeId varchar(20)      ); Query OK, 0 rows affected (0.00 sec)Insert some records in the table using insert command −mysql> insert into DemoTable1799 values('101, 102, 103, 104'); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1799 values('106, 109'); Query OK, 1 row affected (0.00 sec)Display all records from the table using select statement −mysql> select * from DemoTable1799;This will produce the following output:+-----------------+ | EmployeeId      | +-----------------+ | 101, 102, 103, ... Read More

Select some data from a database table and insert into another table in the same database with MySQL

AmitDiwan
Updated on 23-Dec-2019 12:08:26

554 Views

To insert data from a table to another, use INSERT INTO statement. Let us first create a table −mysql> create table DemoTable1      (      Id int,      FirstName varchar(20),      Age int      ); Query OK, 0 rows affected (0.00 sec)Insert some records in the table using insert command −mysql> insert into DemoTable1 values(101, 'Chris', 24); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1 values(102, 'David', 28); Query OK, 1 row affected (0.00 sec)Display all records from the table using select statement −mysql> select * from DemoTable1;This will produce the following ... Read More

How to auto increment with 1 after deleting data from a MySQL table?

AmitDiwan
Updated on 25-Feb-2020 13:18:07

637 Views

For this, you can use TRUNCATE TABLE command. Let us first create a table −mysql> create table DemoTable1796      (      StudentId int NOT NULL AUTO_INCREMENT PRIMARY KEY,      StudentName varchar(20)      ); Query OK, 0 rows affected (0.00 sec)Insert some records in the table using insert command −mysql> insert into DemoTable1796(StudentName) values('Chris Brown'); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1796(StudentName) values('David Miller'); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1796(StudentName) values('John Doe'); Query OK, 1 row affected (0.00 sec)Display all records from the table using select statementmysql> select ... Read More

MySQL query to fetch records from a range of months?

AmitDiwan
Updated on 25-Feb-2020 13:19:16

121 Views

Let us first create a table −mysql> create table DemoTable1795      (      Name varchar(20),      DueDate date      ); Query OK, 0 rows affected (0.00 sec)Insert some records in the table using insert command −mysql> insert into DemoTable1795 values('John', '2018-07-21'); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1795 values('Sam', '2019-10-21'); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1795 values('Sam', '2019-01-10'); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1795 values('Mike', '2018-12-31'); Query OK, 1 row affected (0.00 sec)Display all records from the table using select statement −mysql> ... Read More

How to display two different sums of the same price from column Amount in MySQL?

AmitDiwan
Updated on 23-Dec-2019 12:01:23

105 Views

For this, you can use case statement. Let us first create a table −mysql> create table DemoTable1794      (      Amount int      ); Query OK, 0 rows affected (0.00 sec)Insert some records in the table using insert command −mysql> insert into DemoTable1794 values(100); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1794 values(80); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1794 values(320); Query OK, 1 row affected (0.00 sec)Display all records from the table using select statement −mysql> select * from DemoTable1794;This will produce the following output −+--------+ | Amount | ... Read More

Concatenate two columns when one of such column values is null in MySQL

AmitDiwan
Updated on 23-Dec-2019 12:00:01

1K+ Views

To avoid any issues while running a query, use IFNULL(). Let us first create a table −mysql> create table DemoTable1793      (      StudentFirstName varchar(20),      StudentLastName varchar(20)      ); Query OK, 0 rows affected (0.00 sec)Insert some records in the table using insert command −mysql> insert into DemoTable1793 values('John', 'Smith'); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1793 values('Carol', NULL); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1793 values(NULL, 'Brown'); Query OK, 1 row affected (0.00 sec)Display all records from the table using select statement −mysql> select * from ... Read More

How do I select data that does not have a null record in MySQL?

AmitDiwan
Updated on 23-Dec-2019 11:58:10

63 Views

To select not-null records, use IS NOT NULL property. Let us first create a table −mysql> create table DemoTable1792      (      Name varchar(20)      ); Query OK, 0 rows affected (0.00 sec)Insert some records in the table using insert command −mysql> insert into DemoTable1792 values('John Smith'); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1792 values(NULL); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1792 values('David Miller'); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1792 values(NULL); Query OK, 1 row affected (0.00 sec)Display all records from the table using ... Read More

What would happen if we run SELECT WHERE columnName = zero in MySQL?

AmitDiwan
Updated on 25-Feb-2020 13:21:35

69 Views

The following syntax will fetch all the values from the column −select * from yourTableName where yourColumnName=0;Let us first create a table −mysql> create table DemoTable1791      (      FirstName varchar(20)      ); Query OK, 0 rows affected (0.00 sec)Insert some records in the table using insert command −mysql> insert into DemoTable1791 values('David'); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1791 values('John'); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1791 values('Carol'); Query OK, 1 row affected (0.00 sec)Display all records from the table using select statement −mysql> select * from DemoTable1791;This ... Read More

Get multiple count in a single MySQL query for specific column values

AmitDiwan
Updated on 23-Dec-2019 11:54:48

597 Views

For this, you can use aggregate function sum() along with parameter value for specific column. Let us first create a table −mysql> create table DemoTable1790      (      Id int NOT NULL AUTO_INCREMENT PRIMARY KEY,      Name varchar(20),      Score int      ); Query OK, 0 rows affected (0.94 sec)Insert some records in the table using insert command −mysql> insert into DemoTable1790(Name, Score) values('Chris', 45); Query OK, 1 row affected (0.38 sec) mysql> insert into DemoTable1790(Name, Score) values('David', 55); Query OK, 1 row affected (0.13 sec) mysql> insert into DemoTable1790(Name, Score) values('David', 98); Query OK, ... Read More

Advertisements