在 SQL 查询中将列用作 WHERE 子句中的列表。

huangapple go评论64阅读模式
英文:

Using a column as a list in where clause in a sql query

问题

我在Python中使用列a创建了一个列表。我尝试在SQL查询的where子句中使用该列表。列表是一组账号号码。

从数据框中创建列表

data1
acc_d1 = data1['ACCOUNT_NUMBER']
t1 = tuple(acc_d1)

我的Python SQL查询代码(我在zeppelin中使用)

sql = """
select id_number from table
where account_number IN {}""".format(t1)
prog_list_d1 = pd.read_sql(sql, dbc)

当我手动输入数字创建列表时

acc_d1 = [12129530695080, 12129530755769, 12129516984649......]
t = tuple(acc_d1)

sql = """
select id_number from table
where account_number IN {}""".format(t)
prog_list_d1 = pd.read_sql(sql, dbc)

它可以正常工作。我在一个Zeppelin笔记本中使用Python,并且数据是从Oracle数据库中提取的。

英文:

I created a list using a column a in python. I am trying to that in where clause in a sql query. list is a list of account numbers.

creating a list from the df

data1
acc_d1= data1['ACCOUNT_NUMBER']
t1 = tuple(acc_d1)

my code for sql query in python (I am using zeppelin)

sql="""
select id_number from table
where account_number IN {}""".format(t1)
prog_list_d1 = pd.read_sql(sql, dbc)

when I create a list by manually typing the numbers

acc_d1 = [12129530695080,12129530755769,12129516984649......]
t = tuple(acc_d1)

sql="""
select id_number from table
where account_number IN {}""".format(t)
prog_list_d1 = pd.read_sql(sql, dbc)

it works just fine. I am using python in a zeppelin notebook, and data is pulling from an Oracle database

答案1

得分: 1

你需要在列表周围加上括号。我不懂Python,但我猜应该是这样的:

sql = """
select id_number from table
where account_number IN ({})
""".format(t)

顺便说一下,尽量避免使用这种模式。在IN子句中使用不同长度的列表会导致游标共享出现大问题,并且对共享池造成影响。如果这种情况频繁发生,你的数据库管理员会很不高兴。最好的方法是逐个提取账号(使用真正的绑定变量,而不是字符串替换),或者如果需要处理数百万个账号,可以将账号加载到临时表中,然后从临时表中使用连接获取所有你想要的行,而不需要在SQL中列出它们。

英文:

You will need parentheses around the list. I don't know Python but I would guess it would be simply:

sql="""
select id_number from table
where account_number IN ({})""".format(t)

And, by the way, really try to avoid this pattern. Varying length lists in IN clauses causes big problems for cursor sharing and is hard on the shared pool. Your DBA will not be happy if this happens with high frequency. It is far better to pull one account number at a time (with real bind variables, not string replacement), or if you need millions then load a temp table with the account numbers then use a join from there to your main table to get all the rows you want in one pull, without listing them in the SQL itself.

答案2

得分: 0

数据框中的列是一个对象在转换为列表之前将列的类型更改为字符串就可以了其他部分保持不变

data4['account_number'] = data4['account_number'].astype(str)
prog_d4 = list(data4['account_number'])
prog_d4 = tuple(prog_d4)

sql = """
select account_number from table
where account_number IN {}""".format(prog_d4)
prog_list_d4 = pd.read_sql(sql, dbc)
英文:

The column in the df was an object. By changing the column type to string before converting it to list worked. I kept everything else the same.

data4['account_number'] = data4['account_number'].astype(str)
prog_d4 = list(data4['account_number'])
prog_d4 = tuple(prog_d4)

sql="""
select account_number from table
where account_number IN {}""".format(prog_d4)
prog_list_d4 = pd.read_sql(sql, dbc)

huangapple
  • 本文由 发表于 2023年2月7日 03:42:28
  • 转载请务必保留本文链接:https://go.coder-hub.com/75365850.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定