Found 6702 Articles for Database

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

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

254 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

173 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

172 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

452 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

MongoDB query to find a value from JSON like data?

Smita Kapse
Updated on 30-Jul-2019 22:30:26

4K+ Views

To fund a value from JSON data, use the find() along with dot(.) notation. Let us first create a collection with documents −> db.findValueFromJsonDemo.insertOne(    {       "UserDetails": [{          "_id": new ObjectId(),          "UserName": "Carol",          "UserMessage": "Hi"       }],       "UserFriendsName": ["John", "Sam"]    } ); {    "acknowledged" : true,    "insertedId" : ObjectId("5cdf8a4cbf3115999ed511fd") }Following is the query to display all documents from a collection with the help of find() method −> db.findValueFromJsonDemo.find().pretty();This will produce the following output −{    "_id" : ... Read More

MongoDB Limit fields and slice projection together?

Anvi Jain
Updated on 30-Jul-2019 22:30:26

209 Views

Use the $slice operator. Let us first create a collection with documents −> db.limitAndSliceProjectionDemo.insertOne(    {       "_id" : 101,       "UserName" : "Carol",       "UserAge" : 26,       "UserMesssage" : [          "Hi",          "Hello",          "Bye",          "Awesome",          "Good",          "Bad",          "Nice",          "Good Night",          "Good Morning"       ]    } ); { "acknowledged" : true, "insertedId" : ... Read More

How to add a sub-document to sub-document array in MongoDB?

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

2K+ Views

Use the $push operator to add a sub-document. Let us first create a collection with documents −> db.subDocumentToSubDocumentDemo.insertOne(    {       "_id" :101,       "StudentName" : "Larry",       "StudentAge" : 21,       "StudentDetails" : [          {             "StudentCountryName" : "US",             "StudentFavouriteSubjectList" : [ ]          }       ]    } ); { "acknowledged" : true, "insertedId" : 101 }Following is the query to display all documents from a collection with the help ... Read More

Check if a list is not empty in MongoDB?

Smita Kapse
Updated on 30-Jul-2019 22:30:26

341 Views

For this, use the $size operator. Let us first create a collection with documents −> db.checkIfListIsNotEmptyDemo.insertOne({"UserFriendGroup":["John", "David"]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cdd99e8bf3115999ed511f7") } > db.checkIfListIsNotEmptyDemo.insertOne({"UserFriendGroup":["Carol"]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cdd99e9bf3115999ed511f8") } > db.checkIfListIsNotEmptyDemo.insertOne({"UserFriendGroup":[]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cdd99ebbf3115999ed511f9") } > db.checkIfListIsNotEmptyDemo.insertOne({"UserFriendGroup":[null]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cdd99f2bf3115999ed511fa") } > db.checkIfListIsNotEmptyDemo.insertOne({"UserFriendGroup":[]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cdd99f6bf3115999ed511fb") }Following is the query to display all documents from a collection with the help of find() method −> db.checkIfListIsNotEmptyDemo.find().pretty();This will produce the following output −{   ... Read More

Advertisements