Found 4378 Articles for MySQL

MySQL query to select date >= current date - 3 weeks?

George John
Updated on 30-Jul-2019 22:30:26

678 Views

Use the concept of DATE_SUB(). Let us first create a table −mysql> create table DemoTable    (    Id int NOT NULL AUTO_INCREMENT PRIMARY KEY,    ArrivalDate datetime    ); Query OK, 0 rows affected (1.02 sec)Note: Let’s say the current date is 2019-06-08Insert some records in the table using insert command −mysql> insert into DemoTable(ArrivalDate) values('2019-05-15'); Query OK, 1 row affected (0.13 sec) mysql> insert into DemoTable(ArrivalDate) values('2019-06-08'); Query OK, 1 row affected (0.24 sec) mysql> insert into DemoTable(ArrivalDate) values('2019-05-20'); Query OK, 1 row affected (0.11 sec) mysql> insert into DemoTable(ArrivalDate) values('2019-05-12'); Query OK, 1 row affected (0.12 sec)Display ... Read More

Delete all rows except only a specific row in MySQL?

Ankith Reddy
Updated on 30-Jul-2019 22:30:26

991 Views

To delete all rows except a single specific row, try the below syntax −delete from yourTableName where yourColumnName!=yourValue;Let us first create a table −mysql> create table DemoTable    (    Id int NOT NULL AUTO_INCREMENT PRIMARY KEY,    Subject varchar(100)    ); Query OK, 0 rows affected (0.65 sec)Insert some records in the table using insert command −mysql> insert into DemoTable(Subject) values('MySQL'); Query OK, 1 row affected (0.16 sec) mysql> insert into DemoTable(Subject) values('MongoDB'); Query OK, 1 row affected (0.18 sec) mysql> insert into DemoTable(Subject) values('Java'); Query OK, 1 row affected (0.12 sec)Display all records from the table using select ... Read More

MySQL query to insert current date plus specific time?

Arjun Thakur
Updated on 30-Jul-2019 22:30:26

346 Views

You can use CONCAT() for this. The syntax is as follows −insert into DemoTable values(concat(curdate(), ' yourSpecificTime’));Let us first create a table −mysql> create table DemoTable    (    ArrivalDate datetime    ); Query OK, 0 rows affected (1.06 sec)Insert some records in the table using insert command. We are adding the current date and time −mysql> insert into DemoTable values(concat(curdate(), ' 10:20:05')); Query OK, 1 row affected (0.19 sec) mysql> insert into DemoTable values(concat(curdate(), ' 12:05:00')); Query OK, 1 row affected (0.17 sec)Display all records from the table using select statement −mysql> select *from DemoTable;Output+---------------------+ | ArrivalDate ... Read More

How to delete '\' entry in MySQL?

Ankith Reddy
Updated on 30-Jul-2019 22:30:26

68 Views

You can use delete command −delete from yourTableName where yourColumnName='\\';Let us first create a table −mysql> create table DemoTable    (    FolderName varchar(100)    ); Query OK, 0 rows affected (0.67 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values("????"); Query OK, 1 row affected (0.44 sec) mysql> insert into DemoTable values("\\"); Query OK, 1 row affected (0.13 sec)Display all records from the table using select statement −mysql> select *from DemoTable;Output+------------+ | FolderName | +------------+ | ???? | | \ ... Read More

Update all rows by prefixing a line to every text in MySQL?

Ankith Reddy
Updated on 30-Jul-2019 22:30:26

253 Views

For this, use the CONCAT() function. Let us first create a table −mysql> create table DemoTable    (    Id int NOT NULL AUTO_INCREMENT PRIMARY KEY,    Subject varchar(200)    ); Query OK, 0 rows affected (1.36 sec)Insert some records in the table using insert command −mysql> insert into DemoTable(Subject) values('MySQL'); Query OK, 1 row affected (0.17 sec) mysql> insert into DemoTable(Subject) values('MongoDB'); Query OK, 1 row affected (0.13 sec) mysql> insert into DemoTable(Subject) values('Java'); Query OK, 1 row affected (0.12 sec)Display all records from the table using select statement −mysql> select *from DemoTable;Output+----+---------+ | Id | Subject | +----+---------+ ... Read More

MySQL query to convert a string into a month (Number)?

George John
Updated on 30-Jul-2019 22:30:26

172 Views

Use the str_to_date() method −select month(str_to_date(yourColumnName, '%b')) from yourTableName;Let us first create a table −mysql> create table DemoTable    (    Id int NOT NULL AUTO_INCREMENT PRIMARY KEY,    MonthName varchar(100)    ); Query OK, 0 rows affected (0.76 sec)Insert some records in the table using insert command −mysql> insert into DemoTable(MonthName) values('Jan'); Query OK, 1 row affected (0.25 sec) mysql> insert into DemoTable(MonthName) values('Mar'); Query OK, 1 row affected (0.13 sec) Display all records from the table using select statement: mysql> select *from DemoTable;Output+----+-----------+ | Id | MonthName | +----+-----------+ | 1 | Jan ... Read More

MySQL query to convert a string like “1h 15 min” into 75 minutes?

Chandu yadav
Updated on 30-Jul-2019 22:30:26

105 Views

You can use str_to_date() for this conversion. Let us first create a table −mysql> create table DemoTable    (    stringDate varchar(100)    ); Query OK, 0 rows affected (0.86 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values('1h 15 min'); Query OK, 1 row affected (0.24 sec) mysql> insert into DemoTable values('2h 30 min'); Query OK, 1 row affected (0.21 sec)Display all records from the table using select statement −mysql> select *from DemoTable;Output+------------+ | stringDate | +------------+ | 1h 15 min | | 2h 30 min | +------------+ 2 rows in set ... Read More

Can we use {} while creating a MySQL table?

Arjun Thakur
Updated on 30-Jul-2019 22:30:26

115 Views

No, you need to use open and close parenthesis like this ( ) while creating a table. Use the below syntax −CREATE TABLE IF NOT EXISTS yourTableName (    yourColumnName1 dataType1,    .    .    .    .    .    N );Let us first create a table −mysql> CREATE TABLE IF NOT EXISTS DemoTable    (    CustomerId int,    CustomerName varchar(20),    CustomerAge int    ,    PRIMARY KEY(CustomerId)    ); Query OK, 0 rows affected (0.58 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values(1, 'Chris', 25); Query OK, 1 row ... Read More

Get rows with GROUP BY in MySQL?

Ankith Reddy
Updated on 30-Jul-2019 22:30:26

171 Views

Let us first create a table −mysql> create table DemoTable    (    Id int NOT NULL AUTO_INCREMENT PRIMARY KEY,    Subject varchar(20),    Price int    ); Query OK, 0 rows affected (0.64 sec)Insert some records in the table using insert command −mysql> insert into DemoTable(Subject, Price) values('MySQL', 456); Query OK, 1 row affected (0.16 sec) mysql> insert into DemoTable(Subject, Price) values('MySQL', 456); Query OK, 1 row affected (0.12 sec) mysql> insert into DemoTable(Subject, Price) values('MongoDB', 56); Query OK, 1 row affected (0.16 sec) mysql> insert into DemoTable(Subject, Price) values('MongoDB', 60); Query OK, 1 row affected (0.13 sec) mysql> ... Read More

Retrieve from MySQL only if it contains two hyphen symbols?

Chandu yadav
Updated on 30-Jul-2019 22:30:26

451 Views

For this, use the LIKE operator. Let us first create a table:mysql> create table DemoTable    (    Id int NOT NULL AUTO_INCREMENT PRIMARY KEY,    Password varchar(100)    ); Query OK, 0 rows affected (1.27 sec)Insert some records in the table using insert command −mysql> insert into DemoTable(Password) values('John@--123'); Query OK, 1 row affected (0.19 sec) mysql> insert into DemoTable(Password) values('---Carol234'); Query OK, 1 row affected (0.20 sec) mysql> insert into DemoTable(Password) values('--David987'); Query OK, 1 row affected (0.13 sec) mysql> insert into DemoTable(Password) values('Mike----53443'); Query OK, 1 row affected (0.30 sec)Display all records from the table using select ... Read More

Advertisements