AmitDiwan has Published 10740 Articles

Fetch similar ID records from two tables in MySQL

AmitDiwan

AmitDiwan

Updated on 03-Oct-2019 06:46:55

435 Views

Let us first create a table −mysql> create table DemoTable1 (    Id int ); Query OK, 0 rows affected (1.26 sec)Insert some records in the table using insert command −mysql> insert into DemoTable1 values(100); Query OK, 1 row affected (0.20 sec) mysql> insert into DemoTable1 values(110); Query OK, 1 ... Read More

MySQL query to merge rows if Id is the same and display the highest corresponding value from other columns

AmitDiwan

AmitDiwan

Updated on 03-Oct-2019 06:42:08

911 Views

For this, use aggregate function MAX() along with the GROUP BY clause. Let us first create a table −mysql> create table DemoTable (    Id int,    Value1 int,    Value2 int,    Value3 int,    Value4 int ); Query OK, 0 rows affected (0.61 sec)Insert some records in the ... Read More

MySQL query to fetch records with arrangement in the form of numbers and letter like 99S, 50K, etc.?

AmitDiwan

AmitDiwan

Updated on 03-Oct-2019 06:39:32

191 Views

The easiest way to achieve this is by using REGEXP. Let us first create a table −mysql> create table DemoTable (    Id varchar(50) ); Query OK, 0 rows affected (0.77 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values('John123'); Query OK, 1 row affected ... Read More

Replace a specific duplicate record with a new value in MySQL

AmitDiwan

AmitDiwan

Updated on 03-Oct-2019 06:35:45

188 Views

Let us first create a table −mysql> create table DemoTable (    Id int NOT NULL AUTO_INCREMENT PRIMARY KEY,    Name varchar(50) ); Query OK, 0 rows affected (0.62 sec)Insert some records in the table using insert command −mysql> insert into DemoTable(Name) values('Chris'); Query OK, 1 row affected (0.17 sec) ... Read More

Can we use the result of a SUM() function in MySQL WHERE clause

AmitDiwan

AmitDiwan

Updated on 03-Oct-2019 06:32:13

684 Views

We can use the HAVING clause rather than the WHERE in MySQL. Let us first create a table −mysql> create table DemoTable (    Name varchar(50),    Price int ); Query OK, 0 rows affected (0.79 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values('Chris', ... Read More

Display random row from a MySQL table

AmitDiwan

AmitDiwan

Updated on 03-Oct-2019 06:30:50

273 Views

To display a single random row, use the RAND() with LIMIT. Here, LIMIT is used to fetch the number of records, since we want only a single row, therefore use LIMIT 1. Let us first create a table −mysql> create table DemoTable (    Id int NOT NULL AUTO_INCREMENT PRIMARY ... Read More

MySQL date comparison to fetch dates between a given range?

AmitDiwan

AmitDiwan

Updated on 03-Oct-2019 06:20:15

264 Views

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

Conditional WHERE clause in MySQL stored procedure to set a custom value for NULL values

AmitDiwan

AmitDiwan

Updated on 03-Oct-2019 06:18:03

293 Views

To set a custom value for NULL values, use the UPDATE command along with IS NULL property in a stored procedure. Let us first create a table −mysql> create table DemoTable (    Id int,    FirstName varchar(50) ); Query OK, 0 rows affected (0.67 sec)Insert some records in the ... Read More

Autoincrement in MySQL begins from 1? How can we begin it from another number?

AmitDiwan

AmitDiwan

Updated on 03-Oct-2019 06:12:21

1K+ Views

The autoincrement in MySQL gives a unique number every time. By default, it starts at 1. If you want to start from another number, then you need to change the auto-increment value with the help of ALTER command or you can give value at the time of table creation.Let us ... Read More

How to use three conditions in a single MySQL query with id, name and age of students to fetch record of a student?

AmitDiwan

AmitDiwan

Updated on 03-Oct-2019 06:07:31

327 Views

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

Advertisements