【发布时间】:2021-08-31 06:17:27
【问题描述】:
我有两张桌子,
customers表
+----+------------------+
| id | name |
+----+------------------+
| 1 | Bhagwanti Nair |
| 2 | Anuja Rana |
| 3 | Mohana Varrier |
| 4 | Trilochan Talwar |
| 5 | Charuvrat Gandhi |
+----+------------------+
transactions表
+----+----------+--------+
| id | customer | type |
+----+----------+--------+
| 1 | 1 | debit |
| 2 | 3 | debit |
| 3 | 1 | credit |
| 4 | 4 | debit |
| 5 | 1 | credit |
| 6 | 5 | credit |
| 7 | 3 | debit |
| 8 | 4 | credit |
| 9 | 2 | credit |
| 10 | 2 | debit |
+----+----------+--------+
我正在使用 Sqlflite 数据库构建一个 Flutter 应用程序。我的目标是编写一个查询,当我想获取 customers 时输出以下 JSON 结果:
[
{
"id": 1,
"name": "Bhagwanti Nair",
"transactions": [
{
"id": 1,
"customer": 1,
"type": "debit"
},
{
"id": 3,
"customer": 1,
"type": "credit"
},
{
"id": 5,
"customer": 1,
"type": "credit"
}
]
},
... and so on
]
以同样的方式,我想为transactions 做。就像:
[
{
"id": 1,
"type": "debit",
"customer": {
"id": 1,
"name": "Bhagwanti Nair"
}
},
... and so on
]
customers 和 transactions 表之间的关系是一对多。
我的问题:
- 是否可以在 SQLite 数据库中这样做?如果是,请帮助我。
- 如果不是,解决此问题的最佳方法是什么?
任何帮助将不胜感激。祝你有美好的一天!
【问题讨论】: