AmitDiwan has Published 10740 Articles

Is there anything like substr_replace in MySQL?

AmitDiwan

AmitDiwan

Updated on 04-Oct-2019 06:36:38

187 Views

For this, use the INSERT() function from MySQL. The INSERT(str, pos, len, newstr) returns the string str, with the substring beginning at position pos and len characters long replaced by the string newstr. Returns the original string if pos is not within the length of the string.It replaces the rest ... Read More

Implement MySQL conditional GROUP BY with NOT IN to filter records from duplicate column values

AmitDiwan

AmitDiwan

Updated on 04-Oct-2019 06:34:01

128 Views

Let us first create a table −mysql> create table DemoTable (    Name varchar(40),    Score int ); Query OK, 0 rows affected (0.48 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values('Adam', 89); Query OK, 1 row affected (0.12 sec) mysql> insert into DemoTable ... Read More

Finding average marks of students for different subjects and display only the highest average marks in MySQL

AmitDiwan

AmitDiwan

Updated on 04-Oct-2019 06:32:27

1K+ Views

For this, you can use subquery. Let us first create a table −mysql> create table DemoTable (    StudentName varchar(40),    StudentMarks int ); Query OK, 0 rows affected (0.50 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values('Adam', 56); Query OK, 1 row affected ... Read More

MySQL query to get count of each fileid entry in a table with Id and FileIDs?

AmitDiwan

AmitDiwan

Updated on 03-Oct-2019 08:56:35

112 Views

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

Create a table if it does not already exist and insert a record in the same query with MySQL

AmitDiwan

AmitDiwan

Updated on 03-Oct-2019 07:57:55

2K+ Views

Use CREATE TABLE IF NOT EXISTS for this as shown in the below syntax −create table if not exists yourTableName (    yourColumnName1 dataType,    yourColumnName2 dataType,    yourColumnName3 dataType,    .    .    N ) as select yourValue1 as yourColumnName1 , yourValue2 as yourColumnName2 , yourValue3 as yourColumnName3, ... Read More

SUM a column based on a condition in MySQL

AmitDiwan

AmitDiwan

Updated on 03-Oct-2019 07:55:03

621 Views

Let us first create a table −mysql> create table DemoTable (    CustomerId int NOT NULL AUTO_INCREMENT PRIMARY KEY,    ProductAmount int,    CustomerCountryName varchar(10) ); Query OK, 0 rows affected (0.86 sec)Insert some records in the table using insert command −mysql> insert into DemoTable(ProductAmount, CustomerCountryName) values(190, 'US'); Query OK, ... Read More

How to display a single quote text as a column value in MySQL?

AmitDiwan

AmitDiwan

Updated on 03-Oct-2019 07:51:46

304 Views

To display a single quote text, use double quotes like if you want to write You’ve, then write You've while inserting i.e. with double-quotes. Let us first create a table −mysql> create table DemoTable (    Note text ); Query OK, 0 rows affected (0.57 sec)Insert some records in the ... Read More

Updating blank cells to NULL will cause all cells to be NULL in MySQL?

AmitDiwan

AmitDiwan

Updated on 03-Oct-2019 07:49:27

117 Views

To update only blank cells to NULL, use NULLIF() in MySQL. Let us first create a table −mysql> create table DemoTable (    Name varchar(50) ); Query OK, 0 rows affected (1.73 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values('Mike'); Query OK, 1 row ... Read More

How to implement MAX(distinct…) in MySQL and what is the difference without using DISTINCT?

AmitDiwan

AmitDiwan

Updated on 03-Oct-2019 07:47:26

3K+ Views

Let us see the first syntax, which uses DISTINCT in MAX() −select max(DISTINCT yourColumnName) from yourTableName;The second syntax is as follows. It isn’t using DISTINCT −select max( yourColumnName) from yourTableName;NOTE − Both the above queries give the same result with or without a DISTINCT keyword. MySQL internally converts MAX(yourColumnName) to ... Read More

In MySQL stored procedures, how to check if a local variable is null?

AmitDiwan

AmitDiwan

Updated on 03-Oct-2019 07:43:52

558 Views

For this, use COALESCE(). Let us implement a stored procedure to check if the local variable is null −mysql> DELIMITER // mysql> CREATE PROCEDURE local_VariableDemo()    BEGIN    DECLARE value1 int;    DECLARE value2 int;    select value1, value2;    select    concat('After checking local variable is null the sum ... Read More

Advertisements