【问题标题】:Matching tuples in sqlsql中的匹配元组
【发布时间】:2018-10-10 11:35:55
【问题描述】:

我正在尝试编写一个与列表中的列元组匹配的查询。所以假设我有名字和姓氏的列表,我想匹配数据库中同一索引的名字和姓氏的组合。

first_names = ["Joe", "Freddy", "Michael"]
last_names = ["Jason", "Kruger", "Myers"]

在这种情况下,我希望查询返回某个其他列以获取名称为“Joe Jason”、“Freddy Kruger”或“Michael Myers”的记录。

对我来说,显而易见的方法是按名字和姓氏分组并使用组 concat 然后匹配连接字段。但我想尽量避免这种情况。无论如何,元组匹配是否可以在 SQL 中完成?

【问题讨论】:

  • 请用您正在使用的数据库标记您的问题。

标签: sql select group-by tuples


【解决方案1】:

有些数据库允许您将此逻辑表达为:

where (firstname, lastname) in ( ('Joe', 'Jason'), ('Freddy', 'Kruger'),  ('Michael, 'Myers') )

总而言之,您可以使用布尔逻辑来表达这一点:

where (firstname = 'Joe' and lastname = 'Jason') or
      (firstname = 'Freddy' and lastname = 'Kruger') or
      (firstname = 'Michael' and lastname = 'Myers')

【讨论】:

    猜你喜欢
    • 2020-06-19
    • 1970-01-01
    • 2011-03-03
    • 2022-01-03
    • 1970-01-01
    • 1970-01-01
    • 2014-08-27
    • 2015-05-06
    • 1970-01-01
    相关资源
    最近更新 更多