Found 6702 Articles for Database

MySQL select dates in 30-day range?

karthikeya Boyini
Updated on 30-Jul-2019 22:30:25

1K+ Views

To select dates in 30-day range, you can use arithmetic operation - with interval.The syntax is as follows −select *from yourTableName where yourDateColumnName > NOW() - INTERVAL 30 DAY and yourDateColumnName < NOW() + INTERVAL 30 DAY;To understand the above syntax, let us create a table. The query to create a table is as follows −mysql> create table selectDatesDemo    -> (    -> Id int NOT NULL AUTO_INCREMENT PRIMARY KEY,    -> ArrivalDate datetime    -> ); Query OK, 0 rows affected (0.77 sec)Now you can insert some records in the table using insert command. The query is as ... Read More

Use JOIN to select record with more than one condition using AND?

Samual Sam
Updated on 30-Jul-2019 22:30:25

96 Views

Let us first create a demo table −mysql> create table selectPerson    -> (    -> PersonId int NOT NULL AUTO_INCREMENT PRIMARY KEY,    -> PersonName varchar(20),    -> PersonFavouriteFruit varchar(60)    -> ); Query OK, 0 rows affected (0.58 sec)Insert some records in the table using insert command. The query is as follows −mysql> insert into selectPerson(PersonName, PersonFavouriteFruit) values('John', 'Banana'); Query OK, 1 row affected (0.14 sec) mysql> insert into selectPerson(PersonName, PersonFavouriteFruit) values('John', 'Blackberry'); Query OK, 1 row affected (0.12 sec) mysql> insert into selectPerson(PersonName, PersonFavouriteFruit) values('John', 'Blueberry'); Query OK, 1 row affected (0.12 sec) mysql> insert into selectPerson(PersonName, ... Read More

Does MySQL eliminate common subexpressions between SELECT and HAVING/GROUP BY clause? How to test it?

karthikeya Boyini
Updated on 30-Jul-2019 22:30:25

43 Views

To test, use the sleep() function.Case 1 −The syntax is as follows −SELECT yourColumnName+sleep(yourIntegerValue) FROM yourTableName GROUP BY yourColumnName+sleep(yourIntegerValue);;Case 2 − You can use another syntax which is as follows −SELECT yourColumnName+sleep(yourIntegerValue) As anyAliasName FROM yourTableName GROUP BY yourAliasName;To understand the above syntaxes, let us create a table. The query to create a table is as follows −mysql> create table sleepDemo    -> (    -> value int    -> ); Query OK, 0 rows affected (1.25 sec)Insert some records in the table using insert command. The query is as follows −mysql> insert into sleepDemo values(40); Query OK, 1 row ... Read More

Is it possible to have View and table with the same name in MySQL?

Samual Sam
Updated on 30-Jul-2019 22:30:25

1K+ Views

No, you cannot give the same name for view and table in MySQL.Let us first create a demo table −mysql> create table view_Table_Demo    -> (    -> Id int,    -> Name varchar(20)    -> ); Query OK, 0 rows affected (0.80 sec)Now you can insert some records in the table using insert command. The query is as follows −mysql> insert into view_Table_Demo values(100, 'Larry'); Query OK, 1 row affected (0.17 sec) mysql> insert into view_Table_Demo values(101, 'Mike'); Query OK, 1 row affected (0.20 sec) mysql> insert into view_Table_Demo values(102, 'Sam'); Query OK, 1 row affected (0.14 sec)Display all ... Read More

MySQL server version for the right syntax to use near 'OPTION SQL_SELECT_LIMIT=10'?

Samual Sam
Updated on 30-Jul-2019 22:30:25

672 Views

You can use SET command, but SET OPTIOn deprecated. Therefore, use SET SQL_SELECT_LIMIT.The syntax is as follows −SET SQL_SELECT_LIMIT=yourIntegerValue;To understand the above syntax, let us create a table. The query to create a table is as follows −mysql> create table MySQLSelectDemo    -> (    -> Id int NOT NULL AUTO_INCREMENT PRIMARY KEY    -> ); Query OK, 0 rows affected (0.99 sec)Insert some records in the table using insert command. The query is as follows −mysql> INSERT INTO MySQLSelectDemo VALUES(), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), ... Read More

Getting the highest value of a column in MongoDB?

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

850 Views

To get the highest value of a column in MongoDB, you can use sort() along with limit(1). The syntax is as follows −db.yourCollectionName.find().sort({"yourFieldName":-1}).limit(1);To understand the above syntax, let us create a collection with the document. The query to create a collection with a document is as follows −> db.gettingHighestValueDemo.insertOne({"Value":1029}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c900b885705caea966c5574") } > db.gettingHighestValueDemo.insertOne({"Value":3029}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c900b8d5705caea966c5575") } > db.gettingHighestValueDemo.insertOne({"Value":1092}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c900b925705caea966c5576") } > db.gettingHighestValueDemo.insertOne({"Value":18484}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c900b955705caea966c5577") } > db.gettingHighestValueDemo.insertOne({"Value":37474}); {    "acknowledged" ... Read More

MongoDB query with fields in the same document?

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

121 Views

You can use $where operator for this. To understand the concept, let us create a collection with the document. The query to create a collection with a document is as follows −> db.queryInSameDocumentsDemo.insertOne({"StudentDetails":{"StudentName":"John"}, "NewStudentDetails":{"StudentName":"Carol"}}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c90096ed3c9d04998abf017") } > db.queryInSameDocumentsDemo.insertOne({"StudentDetails":{"StudentName":"Bob"}, "NewStudentDetails":{"StudentName":"Bob"}}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c900a435705caea966c5573") }Display all documents from a collection with the help of find() method. The query is as follows −> db.queryInSameDocumentsDemo.find().pretty();The following is the output −{    "_id" : ObjectId("5c90096ed3c9d04998abf017"),    "StudentDetails" : {       "StudentName" : "John"    },    "NewStudentDetails" : { ... Read More

In MongoDB how do you use $set to update a nested value/embedded document?

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

1K+ Views

The syntax is as follows for this −db.yourCollectionName.update({ }, { $set: { "yourOuterFieldName.yourInnerFieldName": "yourValue" } });To understand the syntax, let us create a collection with the document. The query to create a collection with a document is as follows −> db.updateNestedValueDemo.insertOne({"CustomerName":"Chris",    ... "CustomerDetails":{"CustomerAge":25, "CustomerCompanyName":"Google", "CustomerCityName":"US"}}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c8fccc4d3c9d04998abf015") }Display all documents from a collection with the help of find() method. The query is as follows −> db.updateNestedValueDemo.find().pretty();The following is the output −{    "_id" : ObjectId("5c8fccc4d3c9d04998abf015"),    "CustomerName" : "Chris",    "CustomerDetails" : {       "CustomerAge" : 25,       ... Read More

List all values of a certain field in MongoDB?

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

1K+ Views

To get the list of all values of certain fields in MongoDB, you can use distinct(). The syntax is as follows −db.yourCollectionName.distinct( "yourFieldName");To understand the above syntax, let us create a collection with the document. The query to create a collection with a document is as follows −> db.listAllValuesOfCeratinFieldsDemo.insertOne({"ListOfValues":[10, 20, 30]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c8fc89ed3c9d04998abf011") } > db.listAllValuesOfCeratinFieldsDemo.insertOne({"ListOfValues":[40, 50, 60]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c8fc8abd3c9d04998abf012") } > db.listAllValuesOfCeratinFieldsDemo.insertOne({"ListOfValues":[10, 20, 30]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c8fc8d7d3c9d04998abf013") } > db.listAllValuesOfCeratinFieldsDemo.insertOne({"ListOfValues":[40, 50, 70]}); {    "acknowledged" : true,    "insertedId" ... Read More

How to create a nested index in MongoDB?

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

366 Views

To create nested index in MongoDB, you can use createIndex() or ensureIndex(). The syntax is as follows −db.yourCollectionName.createIndex({"yourOuterFieldName.yourInnerFieldName.yourSecondInnerFieldName": 1});To understand the syntax, let us create a collection with the document. The query to create a collection with a document is as follows −> db.nestedIndexDemo.insertOne(    ... {       ...       ... "CustomerId":101,       ... "CustomerDetails":       ... {          ... "CustomerListDetails":          ... {             ... "CustomerName":"Larry",             ... "CustomerProjectName": "Project-1",           ... Read More

Advertisements