Python:sqlite OR语句 [英] Python: sqlite OR statement

查看:44
本文介绍了Python:sqlite OR语句的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否存在OR sqlite语句?我一直在尝试使用google,但是google似乎正在过滤 OR .

Is there an OR sqlite statement? I've been trying to google it, but google seems to be filtering out the OR.

例如,如果我想在此表中找到某人(人)

For example if I wanted to find someone in this table (people)

people:
+--fname--+--lname--+--has_squirrel--+
|Eric     |Schmidt  |0               |
+---------+---------+----------------+
|Elvis    |Presley  |1               |
+---------+---------+----------------+
|Richard  |Stallman |0               |
+---------+---------+----------------+

名字叫猫王或理查德,我的查询是什么?

with a first name of either Elvis or Richard, what would my query be?

我已经尝试过从诸如fname =('Elvis'或'Richard')的人那里选择 select *之类的东西,但是总是返回 Eric Sc​​hmidt (正好相反)我想要的行)!

I've tried things like select * from people where fname=('Elvis' or 'Richard'), but that always returns Eric Schmidt (the exact opposite rows of what I want)!

我还尝试从fname ='Elvis'或'Richard'的人中进行 select *,但是返回 all 表中的所有行!

I've also tried select * from people where fname='Elvis' or 'Richard' but that returns all the rows in the table!

我正在Ubuntu 10.10上使用Python 2.6 sqlite3模块.有人可以给我关于sqlite或statement的快速解释,还是告诉我它是否不存在?我尝试进行RTFM,却找不到任何东西(在sqlite网站上搜索 OR 会吐出一堆错误!).

I'm using the Python 2.6 sqlite3 module on Ubuntu 10.10. Can someone give me a quick explanation of the sqlite OR statment or tell me if it doesn't exist? I've tried to RTFM and I couldn't find anything (searching for OR on the sqlite website spits out a bunch of errors!).

谢谢!

推荐答案

这将是其中之一:

SELECT * FROM people WHERE fname IN ('Elvis', 'Richard');
SELECT * FROM people WHERE fname = 'Elvis' OR fname = 'Richard';

我很惊讶您的第一个查询仍然有效,而在您的第二个查询中,或'Richard'基本上意味着 or 1 = 1 ,对于每个用户而言,这始终是正确的表格中的行.

I'm surprised your first query even works, and in your second, or 'Richard' basically means or 1=1, which is always true, for every row in your table.

您的计算机不像您那样思考.即使 fname ='Elvis'或'Richard'对您来说似乎很明显,RDBMS也不知道您的意思,而是显示如果fname等于'Elvis'或... true".它将字符串评估为布尔值,并且我假设由于字符串不为空,因此将其评估为true.就像我之前说的,然后继续从表中选择每一行.

Your computer doesn't think like you do. Even if fname = 'Elvis' or 'Richard' seems obvious to you, the RDBMS doesn't know what you mean and reads "if fname is equal to 'Elvis' or... true". It evalutes the string to a boolean, and I assume since it's not empty, it's evaluated to true. And proceeds to select every row from your table, as I said earlier.

这篇关于Python:sqlite OR语句的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆