RethinkDB 使用 Python 加入和排序
RethinkDB Join and Order using Python
我想加入并订购 RethinkDB 中的字段。我的 table 和示例数据是:
类别Table
{
"id": "2be434e0-0f34-4705-ba7f-560437a8e65c" ,
"name": "IT"
} {
"id": "76db46b7-2b1b-4c6e-99dd-83852d921ec0" ,
"name": "Electronic"
} {
"id": "61774bf5-b197-4676-be95-873d6f701243" ,
"name": "Motor"
}
项目table
{
"id": "8d14ac9f-713c-4424-aba8-de2e6fb4d51a" ,
"category_id": "2be434e0-0f34-4705-ba7f-560437a8e65c" ,
"item_name": 'Computer'
}
{
"id": "266f34a7-b850-45b3-b15a-9fb59c90113d" ,
"category_id": "2be434e0-0f34-4705-ba7f-560437a8e65c" ,
"item_name": 'Notebook'
}
{
"id": "397e574c-0597-4198-97c6-33a50c6f464a" ,
"category_id": "2be434e0-0f34-4705-ba7f-560437a8e65c" ,
"item_name": 'Smart Phone'
}
{
"id": "3a080b71-a250-4616-a22b-c14483ce8be0" ,
"category_id": "76db46b7-2b1b-4c6e-99dd-83852d921ec0" ,
"item_name": 'Generator'
}
{
"id": "5a66eb5e-271a-47d6-8c4e-036fa06a0ea2" ,
"category_id": "76db46b7-2b1b-4c6e-99dd-83852d921ec0" ,
"item_name": 'Air-Con'
}
{
"id": "449ec1ef-dac0-42a3-aef9-e79f0556452a" ,
"category_id": "61774bf5-b197-4676-be95-873d6f701243" ,
"item_name": 'Car'
}
我想加入并按 python 中 table 的字段数排序。我想关注结果
{
"id": "2be434e0-0f34-4705-ba7f-560437a8e65c" ,
"name": "IT",
"count": 3
} {
"id": "76db46b7-2b1b-4c6e-99dd-83852d921ec0" ,
"name": "Electronic",
"count": 2
} {
"id": "61774bf5-b197-4676-be95-873d6f701243" ,
"name": "Motor",
'count': 1
}
请帮帮我。
r.table('Category').merge(lambda row:{'count':r.table('Item').filter({'category_id':row['id']}).count()}).order_by(r.desc('count')).run()
我得到了解决方案。谢谢大家
我想加入并订购 RethinkDB 中的字段。我的 table 和示例数据是:
类别Table
{
"id": "2be434e0-0f34-4705-ba7f-560437a8e65c" ,
"name": "IT"
} {
"id": "76db46b7-2b1b-4c6e-99dd-83852d921ec0" ,
"name": "Electronic"
} {
"id": "61774bf5-b197-4676-be95-873d6f701243" ,
"name": "Motor"
}
项目table
{
"id": "8d14ac9f-713c-4424-aba8-de2e6fb4d51a" ,
"category_id": "2be434e0-0f34-4705-ba7f-560437a8e65c" ,
"item_name": 'Computer'
}
{
"id": "266f34a7-b850-45b3-b15a-9fb59c90113d" ,
"category_id": "2be434e0-0f34-4705-ba7f-560437a8e65c" ,
"item_name": 'Notebook'
}
{
"id": "397e574c-0597-4198-97c6-33a50c6f464a" ,
"category_id": "2be434e0-0f34-4705-ba7f-560437a8e65c" ,
"item_name": 'Smart Phone'
}
{
"id": "3a080b71-a250-4616-a22b-c14483ce8be0" ,
"category_id": "76db46b7-2b1b-4c6e-99dd-83852d921ec0" ,
"item_name": 'Generator'
}
{
"id": "5a66eb5e-271a-47d6-8c4e-036fa06a0ea2" ,
"category_id": "76db46b7-2b1b-4c6e-99dd-83852d921ec0" ,
"item_name": 'Air-Con'
}
{
"id": "449ec1ef-dac0-42a3-aef9-e79f0556452a" ,
"category_id": "61774bf5-b197-4676-be95-873d6f701243" ,
"item_name": 'Car'
}
我想加入并按 python 中 table 的字段数排序。我想关注结果
{
"id": "2be434e0-0f34-4705-ba7f-560437a8e65c" ,
"name": "IT",
"count": 3
} {
"id": "76db46b7-2b1b-4c6e-99dd-83852d921ec0" ,
"name": "Electronic",
"count": 2
} {
"id": "61774bf5-b197-4676-be95-873d6f701243" ,
"name": "Motor",
'count': 1
}
请帮帮我。
r.table('Category').merge(lambda row:{'count':r.table('Item').filter({'category_id':row['id']}).count()}).order_by(r.desc('count')).run()
我得到了解决方案。谢谢大家