AmitDiwan has Published 10740 Articles

Prevent a combination of items from being inserted twice in MySQL?

AmitDiwan

AmitDiwan

Updated on 10-Oct-2019 11:54:24

331 Views

To prevent a combination of items from being inserted twice, alter the table and set UNIQUE for the column as shown in the below syntax −alter table yourTableName add constraint yourConstraintName unique(yourColumnName1, yourColumnName2, ....N);Let us first create a table −mysql> create table DemoTable (    Value1 int,    Value2 int ... Read More

Find integer within +/- 1 from a column in MySQL

AmitDiwan

AmitDiwan

Updated on 10-Oct-2019 11:52:23

162 Views

For this, use BETWEEN -1 AND 1. Let us first create a table −mysql> create table DemoTable (    Value int ); Query OK, 0 rows affected (0.76 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values(14); Query OK, 1 row affected (0.15 sec) mysql> ... Read More

How to prevent duplicate rows in MySQL INSERT?

AmitDiwan

AmitDiwan

Updated on 10-Oct-2019 11:50:56

1K+ Views

For this, you need to use UNIQUE KEY for the column. Let us first create a table −mysql> create table DemoTable (    Id int NOT NULL AUTO_INCREMENT PRIMARY KEY,    FirstName varchar(30),    UNIQUE KEY(FirstName) ); Query OK, 0 rows affected (1.76 sec)Insert some records in the table using ... Read More

Fetch records from interval of past 3 days from current date in MySQL and add the corresponding records

AmitDiwan

AmitDiwan

Updated on 10-Oct-2019 11:48:51

2K+ Views

Let us first create a table −mysql> create table DemoTable (    ProductAmount int,    PurchaseDate datetime ); Query OK, 0 rows affected (0.94 sec)Note − Let’s say the current date is 2010-09-15.Insert some records in the table using insert command −mysql> insert into DemoTable values(567, '2019-09-10'); Query OK, 1 ... Read More

MySQL query for text search with LIKE and OR to fetch records

AmitDiwan

AmitDiwan

Updated on 10-Oct-2019 11:45:16

152 Views

Let us first create a table −mysql> create table DemoTable (    Subject text ); Query OK, 0 rows affected (0.86 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values('Introduction to MySQL'); Query OK, 1 row affected (0.31 sec) mysql> insert into DemoTable values('Deep Dive ... Read More

How to select a query for a selected day(2010-11-04) to current date using MySQL?

AmitDiwan

AmitDiwan

Updated on 10-Oct-2019 11:43:16

101 Views

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

Compare date when the AdmissionDate is less than the current date in MySQL

AmitDiwan

AmitDiwan

Updated on 10-Oct-2019 11:41:28

347 Views

Let us first create a table −mysql> create table DemoTable (    AdmissionDate varchar(50) ); Query OK, 0 rows affected (0.63 sec)Note − Let’s say the current date is 14-Sep-2019.Insert some records in the table using insert command. Following is the query −mysql> insert into DemoTable values('15-Sep-2019'); Query OK, 1 ... Read More

Can we implement nested insert with select in MySQL?

AmitDiwan

AmitDiwan

Updated on 10-Oct-2019 11:39:16

692 Views

Yes, we can implement nested insert with select in MySQL as shown in the below syntax −insert into yourTableName2(yourColumnName1, yourColumnName2, .....N) select yourColumnName1, yourColumnName2, ....N from yourTableName1 where yourCondition;Let us first see an example and create a table −mysql> create table DemoTable1 (    Id int NOT NULL AUTO_INCREMENT PRIMARY ... Read More

MySQL CASE WHEN with SELECT to display odd and even ids?

AmitDiwan

AmitDiwan

Updated on 10-Oct-2019 11:36:14

2K+ Views

Let us first create a table −mysql> create table DemoTable (    PageId int ); Query OK, 0 rows affected (0.85 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values(233); Query OK, 1 row affected (0.36 sec) mysql> insert into DemoTable values(34); Query OK, 1 ... Read More

Perform Multi-table delete in MySQL

AmitDiwan

AmitDiwan

Updated on 10-Oct-2019 11:33:23

222 Views

For this, you can use DELETE command. Let us first create a table −mysql> create table DemoTable1 (    Id int,    Name varchar(20) ); Query OK, 0 rows affected (0.63 sec)Insert some records in the table using insert command −mysql> insert into DemoTable1 values(1, 'Chris'); Query OK, 1 row ... Read More

Advertisements