我正在开发一个解析程序,将两个文件中的信息解析为 sqlite3表。
让我们说该表具有以下值: filename,value2,value3,value4
表格中的文件名值绝不会超过两个, 我想写一个 sql查询,加入两行时以下 条件属实:
好的,我正在处理的实际程序有点复杂,所以这可能会更清楚
file1.txt内容
abcd, 1234, efgh klmn, 5678, opqr stuv, 9abc, wxyz
file2.txt内容
abcd, 1234, efgh klmn, 9ffx, opqr stuv, 9abc, wxyz
期望的输出:
答案 0 :(得分:2)
x
是表名
select a.*, b.*
from x as a
inner join x as b
on a.filename<b.filename
and a.value2 = b.value2
and a.value3 = b.value3
and a.value4 = b.value4
union all
select a.*, b.*
from x as a
left join x as b
on a.filename!=b.filename
and a.value2 = b.value2
and a.value3 = b.value3
and a.value4 = b.value4
where b.filename is null
and a.filename = (select min(filename) from x)
union all
select b.*, a.*
from x as a
left join x as b
on a.filename!=b.filename
and a.value2 = b.value2
and a.value3 = b.value3
and a.value4 = b.value4
where b.filename is null
and a.filename = (select max(filename) from x)
分为三部分
min(filename)
仅针对不匹配的左侧文件行进行过滤。max(filename)
仅针对不匹配的右侧文件行进行过滤。除非存在一些可以拼接到查询中的行号列,否则无法准确生成输出顺序(某些固有的排序顺序)。