AmitDiwan has Published 10744 Articles

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

AmitDiwan

AmitDiwan

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

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

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

AmitDiwan

AmitDiwan

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

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

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

AmitDiwan

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

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

AmitDiwan

AmitDiwan

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

121 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'); ... Read More

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

AmitDiwan

AmitDiwan

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

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

How to display first day and last day of the month from date records in MySQL?

AmitDiwan

AmitDiwan

Updated on 18-Dec-2019 06:27:28

294 Views

Let us first create a table −mysql> create table DemoTable    -> (    -> DueDate date    -> ); Query OK, 0 rows affected (0.73 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values('2019-01-11'); Query OK, 1 row affected (0.08 sec) mysql> insert into ... Read More

How do I drop a primary key in MySQL?

AmitDiwan

AmitDiwan

Updated on 18-Dec-2019 06:14:41

522 Views

To drop a primary key, use ALTER at first to alter the table. With that, use DROP to drop the key as in the belowSyntaxalter table yourTableName drop primary key;Let us first create a table −mysql> create table DemoTable    -> (    -> StudentId int NOT NULL,    -> ... Read More

Get database name from a query implemented in a MySQL Stored Procedure?

AmitDiwan

AmitDiwan

Updated on 18-Dec-2019 06:07:21

346 Views

To get the database name, use the below given syntax −select database();Let us implement the above syntax in the stored procedure −mysql> delimiter // mysql> create procedure get_procedure_database_name()    -> begin    -> select concat('The database name=', database());    -> end    -> // Query OK, 0 rows affected (0.34 ... Read More

What is the MySQL query to display the number of tables in a database?

AmitDiwan

AmitDiwan

Updated on 18-Dec-2019 05:52:25

440 Views

Let’s say, here I am using the WEB database. We need to find the number of tables in the database WEB. For this, use the INFORMATION_SCHEMA.TABLES in MySQL.Following is the query to display the number of tables −mysql> select count(table_name) as TotalNumberOfTablesInWebDatabase    -> from information_schema.tables    -> where table_schema='web';This ... Read More

Select and sum with grouping in MySQL?

AmitDiwan

AmitDiwan

Updated on 18-Dec-2019 05:44:33

630 Views

To sum, use the aggregate function SUM(). With that, group using MySQL GROUP BY. Let us first create a table −mysql> create table DemoTable    -> (    -> ProductName varchar(20),    -> ProductQuantity int,    -> ProductPrice int    -> ); Query OK, 0 rows affected (0.63 sec)Insert some ... Read More

Advertisements