我正在尝试从虚构的汽车及其所有者表中检索“ id_owner”列表(例如“ id”,“ owner_id”,“ carname”)。此所有者列表应包含其他列表中的所有汽车。
应为:
SELECT id_owner FROM ownedcars WHERE carname = "Ferrari" or carname = "Mustang" or carname = "Porsche"
我认为需要在子查询中执行此操作,因为我尝试了所有方式,该列表返回了包含此列表中任何汽车的所有id_owners,而不是包含列表中的所有三辆汽车
答案 0 :(得分:1)
这可以通过def _maybe_dedup_names(self, names):
if self.mangle_dupe_cols:
names = list(names)
# counts = defaultdict(int)
counts = defaultdict(lambda:1)
# So that your duplicated column suffix starts with 2 not 1
is_potential_mi = _is_potential_multi_index(names)
for i, col in enumerate(names):
cur_count = counts[col]
while cur_count > 0:
counts[col] = cur_count + 1
if is_potential_mi:
# col = col[:-1] + ('%s.%d' % (col[-1], cur_count),)
col = col[:-1] + ('%s%d' % (col[-1], cur_count),)
else:
# col = '%s.%d' % (col, cur_count)
col = '%s%d' % (col, cur_count)
# eliminate '.' from formating
cur_count = counts[col]
names[i] = col
counts[col] = cur_count + 1
和group by
完成。
having
答案 1 :(得分:0)
我认为它需要在子查询中完成,因为我一直 尝试过,该列表将返回包含此车中任何一个的所有id_owners 列表,而不是包含列表中的所有三辆车
根据以上消息,您可以使用EXISTS运算符实现您的目标。
SELECT id_owner FROM ownedcars T
WHERE EXISTS (SELECT 1 FROM ownedcars WHERE carname='Ferrari' AND id_owner=T.id_owner)
AND EXISTS (SELECT 1 FROM ownedcars WHERE carname='Mustang' AND id_owner=T.id_owner)
AND EXISTS (SELECT 1 FROM ownedcars WHERE carname='Porsche' AND id_owner=T.id_owner)
最好的问候,
会