为什么web2py中两个逻辑相似的查询会给出不同的结果?

2024-05-13 09:22:39 发布

您现在位置:Python中文网/ 问答频道 /正文

我已经为此挣扎了一段时间,只是一时兴起想改变一下状况。为什么它是一种方式而不是另一种?你知道吗

在此表定义上:

db.define_table('bids',
                 Field('body', 'text', label="Application"),
                 Field('selected', 'string', requires=IS_IN_SET(['Yes', 'No']), readable=False, writable=False, default='No', widget=SQLFORM.widgets.radio.widget, label="Select this application"), 
                 Field('confirmed', 'string', requires=IS_IN_SET(['Yes', 'No']), readable=False, writable=False, default='No', widget=SQLFORM.widgets.radio.widget, label="Confirm acceptance"),
                 Field('delivered', 'string', requires=IS_IN_SET(['Yes', 'No']), readable=False, writable=False, default='No'),
                 Field('posted_on', 'datetime', readable=True, writable=False),
                 Field('posted_by', 'reference auth_user', readable=False, writable=False),
                 Field('job_id', 'reference jobs', readable=False, writable=False)
                 )

此查询生成正确的数据

query = db.bids.job_id == job_id and db.bids.delivered=='No' and db.bids.selected =='Yes' and db.bids.confirmed=='Yes'

而这个没有

query = db.bids.job_id == job_id and db.bids.selected =='Yes' and db.bids.confirmed=='Yes' and db.bids.delivered=='No'

Tags: andnoidfalsefielddbstringjob
1条回答
网友
1楼 · 发布于 2024-05-13 09:22:39

这两个查询都不正确,因为您使用了and而不是&(并且未能将每个条件用括号括起来)。应该是:

((db.bids.job_id == job_id) & (db.bids.delivered == 'No') &
 (db.bids.selected == 'Yes') & (db.bids.confirmed == 'Yes'))

原始查询:

db.bids.job_id == job_id and db.bids.delivered=='No' and db.bids.selected =='Yes' and db.bids.confirmed=='Yes'

相当于:

True and True and True and db.bids.confirmed == 'Yes'

在最终查询中只产生一个条件:

db.bids.confirmed == 'Yes'

相关问题 更多 >