Found 6702 Articles for Database

MySQL query to concatenate records with similar corresponding ids in a single row separated by a special character

AmitDiwan
Updated on 06-Apr-2020 13:04:16

217 Views

For this, you can use CONCAT_WS() along with GROUP_CONCAT(). Let us first create amysql> create table DemoTable2016    -> (    -> UserId int,    -> UserName varchar(20)    -> ); Query OK, 0 rows affected (0.51 sec)Insert some records in the table using insert command −mysql> insert into DemoTable2016 values(1, 'Chris'); Query OK, 1 row affected (0.17 sec) mysql> insert into DemoTable2016 values(2, 'Bob'); Query OK, 1 row affected (0.10 sec) mysql> insert into DemoTable2016 values(1, 'David'); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable2016 values(2, 'Carol'); Query OK, 1 row affected (0.12 ... Read More

Perform simple validation in MongoDB?

AmitDiwan
Updated on 06-Apr-2020 14:09:04

188 Views

For validation in MongoDB, use validator. Following is the query to create validation on collection in MongoDB −> db.createCollection( "demo437" , { ...    validator: { $jsonSchema: { ...       bsonType: "object", ...       required: [ "FirstName", "LastName"], ...       properties: { ...          FirstName: { ...             bsonType: "string", ...             description: "This is required" }, ...             LastName: { ...                bsonType: "string", ...         ... Read More

Fetch a specific record using a single MySQL query with AND & OR operator

AmitDiwan
Updated on 06-Apr-2020 14:00:14

109 Views

Let us first create a table −mysql> create table DemoTable2015    -> (    -> StudentId int,    -> StudentName varchar(20),    -> StudentCountryName varchar(20) -> ); Query OK, 0 rows affected (1.20 sec)Insert some records in the table using insert command −mysql> insert into DemoTable2015 values(1, 'Chris', 'US'); Query OK, 1 row affected (0.23 sec) mysql> insert into DemoTable2015 values(2, 'Bob', 'UK'); Query OK, 1 row affected (0.12 sec) mysql> insert into DemoTable2015 values(3, 'David', 'AUS'); Query OK, 1 row affected (0.11 sec) mysql> insert into DemoTable2015 values(4, 'Robert', 'US'); Query OK, 1 ... Read More

Set 'alias' for all the column names in a single MySQL query

AmitDiwan
Updated on 06-Apr-2020 12:55:44

250 Views

To set alias for column names, the syntax is as follows −select yourColumnName1 anyAliasName1, yourColumnName2 anyAliasName2 from yourTableName anyAliasName;To understand the above syntax, let us create a table −mysql> create table DemoTable2014    -> (    -> FirstName varchar(20),    -> LastName varchar(20)    -> ); Query OK, 0 rows affected (0.70 sec)Insert some records in the table using insert command −mysql> insert into DemoTable2014 values('John', 'Smith'); Query OK, 1 row affected (0.17 sec) mysql> insert into DemoTable2014 values('David', 'Miller'); Query OK, 1 row affected (0.16 sec) mysql> insert into DemoTable2014 values('John', 'Doe'); Query OK, ... Read More

Set MySQL select in a custom variable

AmitDiwan
Updated on 06-Apr-2020 12:51:59

328 Views

Let us first create a table −mysql> create table DemoTable2013    -> (    -> Name varchar(20)    -> ); Query OK, 0 rows affected (0.63 sec)Insert some records in the table using insert command −mysql> insert into DemoTable2013 values('Chris'); Query OK, 1 row affected (0.13 sec) mysql> insert into DemoTable2013 values('David'); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable2013 values('Mike'); Query OK, 1 row affected (0.19 sec) mysql> insert into DemoTable2013 values('Sam'); Query OK, 1 row affected (0.10 sec) mysql> insert into DemoTable2013 values('Bob'); Query OK, 1 row affected (0.12 sec)Display all ... Read More

Format date while inserting records in MySQL

AmitDiwan
Updated on 06-Apr-2020 12:50:12

443 Views

To format date while inserting records, use DATE_FORMAT() in the MySQL INSERT statement. Let us first create a table −mysql> create table DemoTable2012    -> (    -> ShippingDate varchar(20)    -> ); Query OK, 0 rows affected (0.48 sec)Insert some records in the table using insert command −mysql> insert into DemoTable2012 values(date_format(curdate(), '%d.%m.%Y')); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable2012 values(date_format(now(), '%d.%m.%Y')); Query OK, 1 row affected (0.13 sec) mysql> insert into DemoTable2012 values(date_format('2014-01-21', '%d.%m.%Y')); Query OK, 1 row affected (0.17 sec)Display all records from the table using select statement −mysql> select *from ... Read More

MySQL stored procedure to execute SHOW CREATE TABLE?

AmitDiwan
Updated on 06-Apr-2020 12:48:14

365 Views

To execute SHOW CREATE TABLE in a stored procedure, use SHOW CREATE TABLE. Let us first create a table −mysql> create table DemoTable2011    -> (    -> StudentId int NOT NULL AUTO_INCREMENT,    -> StudentName varchar(20),    -> StudentAge int,    -> StudentCountryName varchar(20),    -> PRIMARY KEY(StudentId)    -> ); Query OK, 0 rows affected (0.80 sec)Following is the stored procedure executing SHOW CREATE TABLE −mysql> delimiter // mysql> create procedure test_show_create_demo(table_name varchar(100))    -> begin    -> set @query=concat("SHOW CREATE TABLE ", table_name);    -> prepare st from @query;    -> execute st;    -> end   ... Read More

How to merge multiple documents in MongoDB?

AmitDiwan
Updated on 03-Apr-2020 14:22:58

1K+ Views

To merge multiple documents in MongoDB, use aggregate(). Let us create a collection with documents −> db.demo436.insertOne( ...    { ...       "_id" : "101", ...       "Name": "Chris", ...       "details" : [ ...          { ...             "CountryName" : "US", ...             "Age" : 21 ...          } ...       ], ...       "Price" : 50 ...    } ... ); { "acknowledged" : true, "insertedId" : "101" } > db.demo436.insertOne( ... ... Read More

How to append to array in MongoDB?

AmitDiwan
Updated on 03-Apr-2020 14:20:41

261 Views

To append to array in MongoDB, use $concatArrays. Let us create a collection with documents −> db.demo435.insertOne({"FirstName":["Chris"], "LastName":["Brown"]} ); {    "acknowledged" : true,    "insertedId" : ObjectId("5e7719b1bbc41e36cc3cae97") } > db.demo435.insertOne({"FirstName":["David"], "LastName":["Miller"]} ); {    "acknowledged" : true,    "insertedId" : ObjectId("5e7719bdbbc41e36cc3cae98") } > db.demo435.insertOne({"FirstName":["John"], "LastName":["Doe"]} ); {    "acknowledged" : true,    "insertedId" : ObjectId("5e7719c6bbc41e36cc3cae99") }Display all documents from a collection with the help of find() method −> db.demo435.find().pretty();This will produce the following output −{    "_id" : ObjectId("5e7719b1bbc41e36cc3cae97"),    "FirstName" : [       "Chris"       ],       "LastName" : [     ... Read More

MongoDB aggregation / math operation to sum score of a specific student

AmitDiwan
Updated on 03-Apr-2020 14:18:50

197 Views

To sum, use aggregate() along with $sum. Let us create a collection with documents −> db.demo434.insertOne({"Name":"Chris", "Score":45}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e771603bbc41e36cc3cae93") } > db.demo434.insertOne({"Name":"David", "Score":55}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e77161abbc41e36cc3cae94") } > db.demo434.insertOne({"Name":"Chris", "Score":55}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e771624bbc41e36cc3cae95") }Display all documents from a collection with the help of find() method −> db.demo434.find();This will produce the following output −{ "_id" : ObjectId("5e771603bbc41e36cc3cae93"), "Name" : "Chris", "Score" : 45 } { "_id" : ObjectId("5e77161abbc41e36cc3cae94"), "Name" : "David", "Score" : 55 } { "_id" : ObjectId("5e771624bbc41e36cc3cae95"), "Name" : "Chris", ... Read More

Advertisements