Found 6705 Articles for Database

MySQL query to get current datetime and only current date

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

409 Views

The query SELECT NOW() gives the current date as well as current time. If you want only current date, use only CURDATE(). Following is the syntax for datetime −SELECT NOW();The syntax for only date.SELECT CURDATE();Let us now implement the above syntax −Case 1 : If you want both current date and time −mysql> SELECT NOW();Output+-----------------------+ | NOW() | +-----------------------+ | 2019-06-02 15 :30 :39 | +-----------------------+ 1 row in set (0.00 sec)Case 2 : If you want the current date only −mysql> SELECT CURDATE();Output+------------+ | CURDATE() | +------------+ | 2019-06-02 | +------------+ 1 row in set (0.00 sec)

Check for null in MongoDB?

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

590 Views

We will use the Null type here. Following are the null types with the alias −TypeNumberAliasDouble1“double”String2“string”Object3“object”Array4“array”Binary data5“binData”Undefined6“undefined”ObjectId7“objectId”Boolean8“bool”Date9“date”Null10“null”Regular Expression11“regex”Following is the syntax for type 10 i.e. null −db.yourCollectionName.find({"yourFieldName":{ $type: 10 } });The above syntax will find only those documents which have null value. Let us first create a collection with documents −> db.mongoDbEqualDemo.insertOne({"Age":34}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cd7e9121a844af18acdffa3") } > db.mongoDbEqualDemo.insertOne({"Age":""}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cd7e9161a844af18acdffa4") } > db.mongoDbEqualDemo.insertOne({"Age":null}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cd7e9191a844af18acdffa5") } > db.mongoDbEqualDemo.insertOne({"Age":56}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cd7e91e1a844af18acdffa6") } > db.mongoDbEqualDemo.insertOne({}); ... Read More

Add two weeks to a date in MySQL?

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

790 Views

To add two weeks to a date in MySQL, use DATE_ADD() −insert into yourTableName(yourColumnName) values(date_add(now(), interval 2 week));Let us first create a table −mysql> create table DemoTable    (    Id int NOT NULL AUTO_INCREMENT PRIMARY KEY,    ShippingDate datetime    ); Query OK, 0 rows affected (0.62 sec)Following is the query to get the current date −mysql> select now(); +-----------------------+ | now() | +-----------------------+ | 2019-06-02 10 :36 :49 | +-----------------------+ 1 row in set (0.00 sec)Insert some records in the table using ... Read More

MySQL query to find all rows where ID is divisible by 4?

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

546 Views

Let us first create a table with one of the column as ID −mysql> create table DemoTable    (    ID int,    StudentName varchar(10),    CountryName varchar(20)    ); Query OK, 0 rows affected (0.70 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values(0, 'David', 'AUS'); Query OK, 1 row affected (0.24 sec) mysql> insert into DemoTable values(3, 'Chris', 'UK'); Query OK, 1 row affected (0.27 sec) mysql> insert into DemoTable values(8, 'Carol', 'US'); Query OK, 1 row affected (0.26 sec) mysql> insert into DemoTable values(9, 'Sam', 'US'); Query OK, 1 row affected (0.14 ... Read More

How can I drop a collection in MongoDB with two dashes in the name?

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

442 Views

Let us first see the syntax to drop a collection −db.getCollection("yourCollectionNameWithTwoDashes").drop();For demo, we will create a collection name with two dashes as shown below −> db.createCollection("company--EmployeeInformation"); { "ok" : 1 }Create the above collection “company--EmployeeInformation “ with documents. Following is the query:>db.getCollection("company--EmployeeInformation").insertOne({"CompanyName":"Amazon", "EmployeeName":"Chris"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cd7c5ff6d78f205348bc654") } >db.getCollection("company--EmployeeInformation").insertOne({"CompanyName":"Google", "EmployeeName":"Robert"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cd7c60b6d78f205348bc655") }Following is the query to display all documents from a collection with the help of find() method −> db.getCollection("company--EmployeeInformation").find();This will produce the following output −{ "_id" : ObjectId("5cd7c5ff6d78f205348bc654"), "CompanyName" : "Amazon", "EmployeeName" : "Chris" } { ... Read More

How to generate field in MySQL SELECT?

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

176 Views

Use keyword AS for this. Let us first create a table −mysql> create table DemoTable    (    Id int NOT NULL AUTO_INCREMENT PRIMARY KEY,    Name varchar(20)    ); Query OK, 0 rows affected (3.16 sec)Insert some records in the table using insert command −mysql> insert into DemoTable(Name) values('John'); Query OK, 1 row affected (0.50 sec) mysql> insert into DemoTable(Name) values('Robert'); Query OK, 1 row affected (0.29 sec) mysql> insert into DemoTable(Name) values('David'); Query OK, 1 row affected (0.54 sec)Display all records from the table using select statement −mysql> select *from DemoTable;Output+----+--------+ | Id | Name | ... Read More

How to order by a custom rule like order like 4,2,1,3 in MySQL?

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

272 Views

To order with a custom rule, use the ORDER BY FIELD(). Let us first create a table −mysql> create table DemoTable    (    Number int    ); Query OK, 0 rows affected (0.55 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values(1); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable values(4); Query OK, 1 row affected (0.22 sec) mysql> insert into DemoTable values(2); Query OK, 1 row affected (0.15 sec) mysql> insert into DemoTable values(3); Query OK, 1 row affected (0.14 sec)Display all records from the table using select statement −mysql> ... Read More

Combining multiple rows into a comma delimited list in MySQL?

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

4K+ Views

To combine multiple rows into a comma delimited list, use the GROUP_CONCAT() method. Let us first create a table −mysql> create table DemoTable    (    Id int NOT NULL AUTO_INCREMENT PRIMARY KEY,    Name varchar(30),    Marks int    ); Query OK, 0 rows affected (0.52 sec)Insert some records in the table using insert command −mysql> insert into DemoTable(Name, Marks) values('John', 67); Query OK, 1 row affected (0.25 sec) mysql> insert into DemoTable(Name, Marks) values('Carol', 69); Query OK, 1 row affected (0.18 sec) mysql> insert into DemoTable(Name, Marks) values('Sam', 69); Query OK, 1 row affected (0.12 sec) mysql> insert ... Read More

Implementing MongoDB $exists and $ne?

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

343 Views

The $exists is used to check whethre a filed exists, whereas $ne is for not equal condition. Let us first create a collection with documents −> db.existsDemo.insertOne({"Name":"Chris", "Age":21}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cd7c3916d78f205348bc650") } > db.existsDemo.insertOne({"Name":"", "Age":null}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cd7c39a6d78f205348bc651") } > db.existsDemo.insertOne({"Name":null, "Age":24}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cd7c3a66d78f205348bc652") } > db.existsDemo.insertOne({"Age":23}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cd7c3c36d78f205348bc653") }Following is the query to display all documents from a collection with the help of find() method −> db.existsDemo.find().pretty();This will produce the following output −{    "_id" : ObjectId("5cd7c3916d78f205348bc650"),    "Name" : "Chris",    "Age" : 21 } { "_id" : ObjectId("5cd7c39a6d78f205348bc651"), "Name" : "", "Age" ... Read More

MySQL ORDER BY with different ordering for some of the values as descending and others ascending?

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

173 Views

You can use the field() for this. Let us first create a table −mysql> create table DemoTable    (    Value int    ); Query OK, 0 rows affected (0.80 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values(10); Query OK, 1 row affected (0.18 sec) mysql> insert into DemoTable values(70); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable values(60); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable values(56); Query OK, 1 row affected (0.12 sec) mysql> insert into DemoTable values(81); Query OK, 1 row affected (0.44 sec) mysql> ... Read More

Advertisements