Found 6702 Articles for Database

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

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

394 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

336 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

130 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

205 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

3K+ 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

282 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

115 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

Use ObjectId under findOne() to fetch a specific record in MongoDB?

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

387 Views

Let us first create a collection with documents −> db.findOneWorkingDemo.insertOne({"ClientId":1, "ClientName":"Larry", "ClientAge":26}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cd7c1716d78f205348bc64d") } > db.findOneWorkingDemo.insertOne({"ClientId":2, "ClientName":"Chris", "ClientAge":28}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cd7c17d6d78f205348bc64e") } > db.findOneWorkingDemo.insertOne({"ClientId":3, "ClientName":"Robert", "ClientAge":34}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cd7c1896d78f205348bc64f") }Following is the query to display all documents from a collection with the help of find() method −> db.findOneWorkingDemo.find().pretty();This will produce the following output −{    "_id" : ObjectId("5cd7c1716d78f205348bc64d"),    "ClientId" : 1,    "ClientName" : "Larry",    "ClientAge" : 26 } {    "_id" : ObjectId("5cd7c17d6d78f205348bc64e"),    "ClientId" : 2, ... Read More

How to check whether now() falls between two specific dates in MySQL?

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

407 Views

Here, now() represents the current date. To check whether it falls between two specific dates, you need to use the BETWEEN. Let us first create a table −mysql> create table DemoTable    (    FirstDate datetime,    SecondDate datetime    ); Query OK, 0 rows affected (0.60 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values('2019-04-01', '2019-05-02'); Query OK, 1 row affected (0.23 sec) mysql> insert into DemoTable values('2019-05-28', '2019-06-04'); Query OK, 1 row affected (0.27 sec) mysql> insert into DemoTable values('2016-01-31', '2019-03-01'); Query OK, 1 row affected (0.15 sec)Display all records from the table ... Read More

Get MAX() on column in two MySQL tables?

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

405 Views

Use GREATEST() to find the maximum. Let us first create a table −mysql> create table DemoTable1    (    Number int    ); Query OK, 0 rows affected (0.52 sec)Insert some records in the table using insert command −mysql> insert into DemoTable1 values(80); Query OK, 1 row affected (0.26 sec) mysql> insert into DemoTable1 values(229); Query OK, 1 row affected (0.15 sec) mysql> insert into DemoTable1 values(575); Query OK, 1 row affected (0.24 sec)Display all records from the table using select statement −mysql> select *from DemoTable1;Output+--------+ | Number | +--------+ | 80 | | 229 ... Read More

Advertisements