У меня есть следующие две таблицы в Db, с отношением один ко многим:
self.c.execute("CREATE TABLE IF NOT EXISTS dispatch("
"id_dispatch INTEGER PRIMARY KEY AUTOINCREMENT,"
"d_date TEXT,"
"d_mill TEXT,"
"d_buyer TEXT,"
"d_addFreight INTEGER,"
"d_subFreight INTEGER,"
"d_amount INTEGER,"
"d_cd INTEGER,"
"d_comm INTEGER,"
"d_netAmount INTEGER)")
self.c.execute("CREATE TABLE IF NOT EXISTS dispatch_products("
"dispatch_id INTEGER NOT NULL REFERENCES DISPATCH(id_dispatch),"
"product INTEGER,"
"quantity INTEGER,"
"rate INTEGER)")
Я пытаюсь объединить эти две таблицы в одну и отобразить в одной таблице, используя следующий код:
def loadTable(self):
connection = sqlite3.connect('main_databaseSample.db')
query = "CREATE VIEW myview as SELECT d_buyer," \
"d_date, product, quantity, rate, d_amount, d_addFreight, d_subFreight, d_netAmount from " \
"dispatch, dispatch_products WHERE dispatch.id_dispatch = dispatch_products.dispatch_id"
query = "SELECT * FROM myview"
result = connection.execute(query)
self.tableWidget.setRowCount(0)
for row_number, row_data in enumerate(result):
self.tableWidget.insertRow(row_number)
for column_number, data in enumerate(row_data):
self.tableWidget.setItem(row_number, column_number,QtWidgets.QTableWidgetItem(str(data)))
connection.close()
ПРОБЛЕМА : заказы с несколькими товарами повторяются.Как исключить несколько записей, которые имеют отношение один ко многим?
Пример данных:
dispatch table:
ID date mill buyer addF subF amount cd comm netAmount
1 15-10 abc A 0 0 100 0 0 100
2 16-10 xyz B 0 0 200 0 0 200
dispatch_products table:
Dispatch_ID product qty rate
1 M 40 1
1 A 60 1
2 S 50 4
Code Output:
buyer date product quantity rate amount addFreight subFreight NetAmount
A 15-10 M 40 1 100 0 0 100
A 15-10 A 60 1 100 0 0 100
B 16-10 S 50 4 200 0 0 200
Expected Output:
buyer date product quantity rate amount addFreight subFreight NetAmount
A 15-10 M 40 1 100 0 0 100
A 60 1
B 16-10 S 50 4 200 0 0 200