[英]Difference between AND and OR in WHERE condition of a SELECT statement in Mysql Database?
I have a database table called employee, When retrieving the name of the employee like this 我有一个名为employee的数据库表,当像这样检索员工的名字时
SELECT * FROM employee WHERE Fname ='jack' AND Fname ='john';
This query returns an empty set. 此查询返回一个空集。 Tried Querying the database using the
OR
condition like this 尝试使用这样的
OR
条件查询数据库
SELECT * FROM employee WHERE Fname ='jack' OR Fname = 'John';
The query returns all the fields of jack and john from the employee table. 查询返回employee表中jack和john的所有字段。 My understanding of
AND
operator is 1 and 1 is true
, where Jack and John is in my table it should return the fields of jack and john from the employee table using the AND
operator. 我对
AND
运算符的理解是1 and 1 is true
,其中Jack和John在我的表中它应该使用AND
运算符从employee表返回jack和john的字段。 Am I allowed to use AND
condition on a single table, Have I misunderstood the concept. 我是否允许在一张桌子上使用
AND
条件,我是否误解了这个概念。 Please some good advice! 请一些好建议!
The condition Fname ='jack' AND Fname ='john'
means the query will match each row where Fname
is both 'jack'
and 'john'
. 条件
Fname ='jack' AND Fname ='john'
表示查询将匹配Fname
为'jack'
和'john'
每一行。 Since a single column can't be two different values at once, this will match no rows. 由于单个列不能同时为两个不同的值,因此不会匹配任何行。
WHERE Fname ='jack' AND Fname ='john'
returns an empty set because there is no record where Fname is both 'jack' AND 'john'. WHERE Fname ='jack' AND Fname ='john'
返回一个空集,因为没有记录Fname是'jack'和'john'。 'jack <> 'john'. 'jack <>'john'。 If you want to return records when the Fname can be either
jack
or john
then use ...WHERE Fname ='jack' OR Fname ='john'
. 如果你想在Fname可以是
jack
或john
时返回记录,那么使用...WHERE Fname ='jack' OR Fname ='john'
。
What you write in the WHERE
clause is a condition . 你在
WHERE
子句中写的是一个条件 。 Writing Fname = 'jack' AND Fname = 'john'
looks for people whose first name is "jack" and at the same time "john". 写
Fname = 'jack' AND Fname = 'john'
寻找名字为“jack”且同时为“john”的人。 But what you want is people whose first name is "jack" or "john". 但你想要的是名字叫“杰克” 或 “约翰”的人。
你想要的是or
因为Fname不能同时是杰克和约翰
SELECT * FROM employee WHERE Fname ='jack' or Fname ='john';
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.