MongoDBの2つの列にまたがってグループ化しますか?
2つの列にグループ化するには、$lookupを使用します。ドキュメントを使用してコレクションを作成しましょう-
> db.demo132.insertOne({"CountryName1":"US","CountryName2":"UK",Value:50});
{
"acknowledged" : true,
"insertedId" : ObjectId("5e31950468e7f832db1a7f75")
}
> db.demo132.insertOne({"CountryName1":"UK","CountryName2":"AUS",Value:10});
{
"acknowledged" : true,
"insertedId" : ObjectId("5e31951d68e7f832db1a7f76")
}
> db.demo132.insertOne({"CountryName1":"AUS","CountryName2":"US",Value:40});
{
"acknowledged" : true,
"insertedId" : ObjectId("5e31952c68e7f832db1a7f77")
}> find()メソッドを使用してコレクションからすべてのドキュメントを表示する-
> db.demo132.find();
これにより、次の出力が生成されます-
{ "_id" : ObjectId("5e31950468e7f832db1a7f75"), "CountryName1" : "US", "CountryName2" : "UK", "Value" : 50 }
{ "_id" : ObjectId("5e31951d68e7f832db1a7f76"), "CountryName1" : "UK", "CountryName2" : "AUS", "Value" : 10 }
{ "_id" : ObjectId("5e31952c68e7f832db1a7f77"), "CountryName1" : "AUS", "CountryName2" : "US", "Value" : 40 } 以下は、MongoDBの2つの列にまたがってグループ化するためのクエリです-
> db.demo132.aggregate( [
... {
... "$lookup" : {
... "from" : "demo132",
... "localField" : "CountryName1",
... "foreignField" : "CountryName2",
... "as" : "out"
... }
... },
... {
... "$unwind" : "$out"
... },
... {
... "$project" : {
... "_id" : 0,
... "CountryName1" : 1,
... "total" : { "$sum" : [ "$Value", "$out.Value"]}
... }
... }
... ]) これにより、次の出力が生成されます-
{ "CountryName1" : "US", "total" : 90 }
{ "CountryName1" : "UK", "total" : 60 }
{ "CountryName1" : "AUS", "total" : 50 } -
MySQLテーブルを2列で並べ替えますか?
以下の構文を使用して、MySQLテーブルを2列で並べ替えます- order by yourColumnName1 DESC,yourColumnName2 DESC; まず、例のテーブルを作成しましょう- mysql> create table OrderByDemo -> ( -> StudentId int, -> StudentName varchar(100), -> StudentAge int -> ); Query OK, 0 rows affected (0.57 sec) 挿入コマンドを使用してテーブルにレ
-
MySQLで2つの列を連結しますか?
2つの列を連結するには、MySQLのCONCAT()関数を使用します。構文は次のとおりです- select CONCAT(yourColumnName1, ' ',yourColumnName2) as anyVariableName from yourTableName; 上記の概念を理解するために、テーブルを作成しましょう。テーブルを作成するためのクエリは次のとおりです- mysql> create table concatenateTwoColumnsDemo −> ( −>