在两个单独的查询中获取增量

时间:2017-01-13 17:05:13

标签: python sql

查询1

Select *
from tableA ta
left join tableB tb on tb.value1=ta.value1
where value1 is like '%something%'

生成此表

value1 value2
a      1
b      2
c      3
d      4

查询2

Select *
from tableC tc
inner join tableD td on tc.value3=td.value4
where value3 is like '%something%else%'

生成表

value3 value4
a      1
b      2
e      5
f      6

如何在查询1和查询2(关于查询1)的表之间获取'delta'?即如何编写查询以便我得到结果

value1 value2
c      3
d      4

我的两个查询都很复杂,所以我放弃了尝试使用,其中值不像'%%'。我认为最好的方法是创建临时表或使用python进行字符串匹配,但我不知道如何做这些事情。

2 个答案:

答案 0 :(得分:1)

你可以做到

Select *
from tableA ta
left join tableB tb on tb.value1=ta.value1
where value1 is like '%something%'
EXCEPT
Select *
from tableC tc
inner join tableD td on tc.value3=td.value4
where value3 is like '%something%else%'

有关此here

的更多信息

答案 1 :(得分:1)

您可以使用class Meds: def __init__(self, file): self.file = file def meds(self): for i in self.file.readlines(): data_split = i.strip().split(' ') hour = data_split[0] temp = data_split[1] dose = data_split[2] yield 'At {0}:00 - he had {1} temp, and took {2} mg of meds'.format(hour, temp, dose) source = open('meds.txt', 'r', encoding='utf8') a = Meds(source) print(list(a.meds())) source.close() 并查找空值(即不会出现在第二个表格中):

LEFT JOIN