AmitDiwan has Published 11365 Articles

Is it compulsory to set PRIMARY KEY for AUTO_INCREMENT value?

AmitDiwan

AmitDiwan

Updated on 04-Oct-2019 06:45:40

120 Views

Yes, use AUTO_INCREMENT with PRIMARY KEY. Let us first create a table −mysql> create table DemoTable (    EmployeeId int NOT NULL AUTO_INCREMENT,    EmployeeName varchar(40),    EmployeeAge int,    PRIMARY KEY(EmployeeId),    UNIQUE KEY(EmployeeName, EmployeeAge) ); Query OK, 0 rows affected (0.96 sec)Let us check the table description of ... Read More

MySQL query to find the average of only first three values from a column with five values

AmitDiwan

AmitDiwan

Updated on 04-Oct-2019 06:42:16

173 Views

For this, you can use a subquery. Let us first create a table −mysql> create table DemoTable (    Score int ); Query OK, 0 rows affected (0.62 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values(80); Query OK, 1 row affected (0.13 sec) mysql> ... Read More

MySQL query to display record with maximum count values in a group with other column values?

AmitDiwan

AmitDiwan

Updated on 04-Oct-2019 06:40:23

300 Views

For this, use the GROUP BY HAVING clause. Let us first create a table −mysql> create table DemoTable (    Value int ); Query OK, 0 rows affected (0.54 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values(88); Query OK, 1 row affected (0.22 sec) ... Read More

How to count specific comma separated values in a row retrieved from MySQL database?

AmitDiwan

AmitDiwan

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

382 Views

To count comma-separated-values, use aggregate function COUNT(*) along with FIND_IN_SET(). Let us first create a table −mysql> create table DemoTable (    Value varchar(100) ); Query OK, 0 rows affected (0.49 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values('10, 20, 60, 80'); Query OK, ... Read More

Is there anything like substr_replace in MySQL?

AmitDiwan

AmitDiwan

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

124 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

84 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

847 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

60 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

1K+ 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

464 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

Advertisements