简体   繁体   English

如何将sqlite3数据库中的所有表合并为一个,但仅包含唯一内容? (不包括相同的行数据)

[英]How can I combine all the tables in a sqlite3 database into one, but only including the unique contents? (Excluding same row data)

FOr example, I have 4 tables, and in table 1 there is a row with the same data as a row in table 2. In table 3 there is a row that looks the same as a row in table 4. I need them all in a single table,( it doesn't matter if it's in the same database or not) but excluding the duplicate rows. 例如,我有4个表,在表1中有一行与表2中的行具有相同的数据。在表3中,有一行看起来与表4中的行相同。我需要它们全部在一个表,(如果它在同一个数据库中,无关紧要),但不包括重复的行。

They all have the same no. 他们都有相同的没有。 of columns and the same column names. 列和相同的列名称。

Please help me create a function, because I have tried UNION But it just does not work, I have also tried UNIQUE, and it also does not work. 请帮我创建一个函数,因为我已经尝试了UNION但它只是不起作用,我也尝试了UNIQUE,它也不起作用。

This is my function that involves UNIQUE: 这是我的功能,涉及UNIQUE:

def create_table():
    c.execute('CREATE TABLE IF NOT EXISTS ring(members INTEGER,distance REAL,UNIQUE(members, distance))')

it always spews out an error. 它总是喷出一个错误。 BUt please, do not focus on my sample code too much, I'm looking for better ways. 请注意,不要太专注于我的示例代码,我正在寻找更好的方法。

Thank you so much. 非常感谢。

I believe the following will do as you wish (table names would need changing, hopefully explanatory table names used) :- 我相信以下内容可以按照您的意愿进行(表名需要更改,希望使用的解释性表名称): -

WITH RECURSIVE all4 AS(
SELECT * FROM 
    ring1
    UNION SELECT * FROM ring2 UNION SELECT * FROM ring3 UNION SELECT * FROM ring4
)
INSERT OR IGNORE INTO newring SELECT * FROM all4 
;
SELECT * FROM newring;

The following demonstrates the above :- 以下说明如下: -

DROP TABLE If EXISTS ring1;
DROP TABLE If EXISTS ring2;
DROP TABLE If EXISTS ring3;
DROP TABLE If EXISTS ring4;
DROP TABLE IF EXISTS newring;
CREATE TABLE IF NOT EXISTS ring1(othercolumn TEXT,members INTEGER,distance REAL);
CREATE TABLE IF NOT EXISTS ring2(othercolumn TEXT,members INTEGER,distance REAL);
CREATE TABLE IF NOT EXISTS ring3(othercolumn TEXT,members INTEGER,distance REAL);
CREATE TABLE IF NOT EXISTS ring4(othercolumn TEXT,members INTEGER,distance REAL);
CREATE TABLE IF NOT EXISTS newring(othercolumn TEXT,members INTEGER,distance REAL, UNIQUE(members,distance));
INSERT INTO ring1 VALUES
    ('A',10,30.5),('B',11,30.6),('C',12,30.7),('D',13,30.8), -- duplicates not the othercolumn though
    ('X',100,15.0),('Y',110,17.0),('Z',120,19.0)
;
INSERT INTO ring2 VALUES
    ('E',10,30.5),('F',11,30.6),('G',12,30.7),('H',13,30.8), -- duplicates not the othercolumn though
    ('X',200,15.0),('Y',210,17.0),('Z',220,19.0)
;
INSERT INTO ring3 VALUES
    ('I',10,30.5),('J',11,30.6),('K',12,30.7),('L',13,30.8), -- duplicates not the othercolumn though
    ('X',300,15.0),('Y',310,17.0),('Z',320,19.0)
;
INSERT INTO ring4 VALUES
    ('M',10,30.5),('N',11,30.6),('O',12,30.7),('P',13,30.8), -- duplicates not the othercolumn though
    ('X',400,15.0),('Y',410,17.0),('Z',420,19.0)
;
WITH RECURSIVE all4 AS(
SELECT * FROM 
    ring1
    UNION SELECT * FROM ring2 UNION SELECT * FROM ring3 UNION SELECT * FROM ring4
)
SELECT * FROM all4 
;
WITH RECURSIVE all4 AS(
SELECT * FROM 
    ring1
    UNION SELECT * FROM ring2 UNION SELECT * FROM ring3 UNION SELECT * FROM ring4
)
INSERT OR IGNORE INTO newring SELECT * FROM all4 
;
SELECT * FROM newring;

The result being :- 结果是: -

在此输入图像描述

That is of the 28 rows 16 were added. 这是28行16添加。

16 as each of the four base tables has 3 unique rows (so 12 rows), each table has 4 rows that have the same member/distance combination. 如图16所示,由于四个基表中的每一个具有3个唯一行(因此12行),每个表具有4行,这些行具有相同的成员/距离组合。

Thus, only 4 of those 16 will be added due to the UNIQUE constraint and the INSERT OR IGNORE, and therefore 16 rows in total added. 因此,由于UNIQUE约束和INSERT OR IGNORE,将仅添加16个中的4个,因此总共添加了16行。

The intermediate query ie the UNION of the 4 tables produces 28 rows as per :- 中间查询,即4个表的UNION,按照以下方式生成28行: -

在此输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何从数据库中读取sqlite3中特定行? - how do I read from a database for a specific row in sqlite3? 将数据插入特定的Sqlite3数据库行吗? - Insert data into specific Sqlite3 database row? 如何在客户端快速访问数据库的情况下使用带有唯一索引的python将项目添加到sqlite3数据库并保留新索引 - How can I add items to an sqlite3 database with python with unique indicies and retain the new index while clients rapidly access the database 使用python和sqlite3合并来自同一数据库的两个SQLite表 - Merging two SQLite tables from the same database with python and sqlite3 将 SQLite3 数据库中的所有表合并到一个 Pandas 数据框中 - Merge all tables from SQLite3 database into one single pandas dataframe 我如何在 Python 中打印行(Sqlite3,Python) - How i can print the row in Python (Sqlite3, Python) SQLITE3 DB数据从同一表和同一行中的一个字段传输到另一个字段 - SQLITE3 DB data transfer from one field to another in the same table and same row 如何将pickle文件中的数据写入sqlite3数据库? - How can I write data from a pickle file to sqlite3 database? 如何将未知数量的数据插入到 sqlite3 数据库中 - How do I Insert unknown amounts of data into a sqlite3 database 如何在Python的sqlite3中搜索整个数据库? - How can I search an entire database in Python's sqlite3?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM