AmitDiwan has Published 10740 Articles

Update the table by calculating the sum and display the result as last column value

AmitDiwan

AmitDiwan

Updated on 04-Oct-2019 08:39:35

701 Views

Use a variable to store SUM(total) and update it with the UPDATE command. Let us first create a table −mysql> create table DemoTable (    Id int NOT NULL AUTO_INCREMENT PRIMARY KEY,    Value int ); Query OK, 0 rows affected (0.61 sec)Insert some records in the table using insert ... Read More

How can I return a record from a table nearest to a user-defined variables value in MySQL?

AmitDiwan

AmitDiwan

Updated on 04-Oct-2019 08:36:23

189 Views

Let us first create a table −mysql> create table DemoTable (    CustomerId int NOT NULL AUTO_INCREMENT PRIMARY KEY,    ProductAmount int ); Query OK, 0 rows affected (0.61 sec)Insert some records in the table using insert command −mysql> insert into DemoTable(ProductAmount) values(5000); Query OK, 1 row affected (0.13 sec) ... Read More

How does COALESCE order results with NULL and NON-NULL values?

AmitDiwan

AmitDiwan

Updated on 04-Oct-2019 08:31:21

835 Views

The COALESCE() finds the NON-NULL value first If it finds the same in the beginning, then it returns, otherwise moves ahead to check NON-NULL value.Let us first create a table −mysql> create table DemoTable (    Number1 int,    Number2 int ); Query OK, 0 rows affected (5.48 sec)Insert some ... Read More

MySQL query to find latest 3 dates in a table and the resultant dates shouldn’t be duplicate

AmitDiwan

AmitDiwan

Updated on 04-Oct-2019 08:28:52

496 Views

To find the latest dates, order the date records with ORDER BY DESC. Since we want only 3 dates, use LIMIT 3.Let us first create a table −mysql> create table DemoTable (    AdmissionDate date ); Query OK, 0 rows affected (0.56 sec)Insert some records in the table using insert ... Read More

Display specific name from a table with repeated individual FirstName and LastName using LIKE clause twice

AmitDiwan

AmitDiwan

Updated on 04-Oct-2019 08:26:25

94 Views

Let us first create a table −mysql> create table DemoTable (    StudentId int NOT NULL AUTO_INCREMENT PRIMARY KEY,    StudentName varchar(30) ); Query OK, 0 rows affected (0.70 sec)Insert some records in the table using insert command −mysql> insert into DemoTable(StudentName) values('John Smith'); Query OK, 1 row affected (0.13 ... Read More

How to extract only the numbers from a text field in MySQL?

AmitDiwan

AmitDiwan

Updated on 04-Oct-2019 08:23:53

1K+ Views

Let us first create a table −mysql> create table DemoTable (    Number text ); Query OK, 0 rows affected (0.49 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values('7364746464, -'); Query OK, 1 row affected (0.21 sec) mysql> insert into DemoTable values('-, 8909094556'); Query ... Read More

MySQL query to convert height format to centimeters?

AmitDiwan

AmitDiwan

Updated on 04-Oct-2019 08:15:01

664 Views

For this, use the CAST() method in MySQL. Let us first create a table −mysql> create table DemoTable (    Id int NOT NULL AUTO_INCREMENT PRIMARY KEY,    StudentHeight varchar(40) ) ; Query OK, 0 rows affected (0.47 sec)Insert some records in the table using insert command −mysql> insert into ... Read More

How to sum varchar column and display the count in MySQL?

AmitDiwan

AmitDiwan

Updated on 04-Oct-2019 08:09:55

776 Views

For this, use GROUP BY along with COUNT(*). Let us first create a table −mysql> create table DemoTable (    EmployeeId int NOT NULL AUTO_INCREMENT PRIMARY KEY,    EmployeeGender varchar(40) ); Query OK, 0 rows affected (0.48 sec)Insert some records in the table using insert command −mysql> insert into DemoTable(EmployeeGender) ... Read More

Format date with DATE_FORMAT() and STR_TO_DATE() in MySQL

AmitDiwan

AmitDiwan

Updated on 04-Oct-2019 08:01:09

353 Views

Let us first create a table −mysql> create table DemoTable (    DueDate varchar(100) ); Query OK, 0 rows affected (0.62 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values('August 04, 2019'); Query OK, 1 row affected (0.25 sec)Display all records from the table using ... Read More

How can I set my auto-increment value to begin from 1 in MySQL?

AmitDiwan

AmitDiwan

Updated on 04-Oct-2019 07:58:49

495 Views

You can truncate the table to set auto_increment value to start from 1 in MySQL. Let us first create a table −mysql> create table DemoTable (    StudentId int NOT NULL AUTO_INCREMENT PRIMARY KEY ); Query OK, 0 rows affected (1.44 sec)Insert some records in the table using insert command ... Read More

Advertisements