-2

我在 SQLLite Studio 中工作并尝试查找具有给定 CUSIP 的所有行。但是,当我添加“where cusip='00080010'”时,我的查询没有返回任何结果,尽管我可以看到第一行有那个 cusip。

我首先考虑到 cusip 实际上可能是一个整数,所以我检查了表定义,它确实被列为 TEXT。

我创建了这个测试查询,发现与我的字符串文字的字符串相等性失败:

select case when cusip='00080010' then 1 else 0 end test, cusip
from msf 
order by date,cusip
limit 1

test    cusip
0       00080010

我尝试在 python 中运行查询,以防这是 ​​SQLLite Studio 的问题,并得到以下结果:

db.execute("""select case when cusip='00080010' then 1 else 0 end test, cusip
    ...: from msf
    ...: order by date,cusip
    ...: limit 1""").fetchone()

(0, b'00080010')

所以,可能它被存储为字节。然后我尝试修改我的查询:

select case when cusip=b'00080010' then 1 else 0 end test, cusip
from msf 
order by date,cusip
limit 1

Error while executing SQL query on database 'a_stock': near "'00080010'": syntax error

第二次尝试:

select case when cusip=cast('00080010' as bytes) then 1 else 0 end test, cusip
from msf 
order by date,cusip
limit 1

test    cusip
0       00080010

在蟒蛇中:

db.execute("""select case when cusip=cast('00080010' as bytes) then 1 else 0 end test, cusip
    ...: from msf
    ...: order by date,cusip
    ...: limit 1""").fetchone()

(0, b'00080010')

我现在没有主意了。为什么字符串比较失败,我该如何解决,有没有办法提前知道这是否会发生?

编辑:抱歉打错了,值是'00080010',我把它输入错误。我重新测试了代码以确保我仍然遇到同样的问题。

4

1 回答 1

0

可以有空格吗?您是否尝试过以下操作?

select case when trim(cusip) = '00080010' then 1 else 0 end test, cusip
from msf 
order by date,cusip
limit 1

或者试试这个来处理任何前导零:

select case when cast(cusip as integer) = 80010 then 1 else 0 end test, cusip
from msf 
order by date,cusip
limit 1
于 2020-08-08T20:09:03.033 回答