简体   繁体   English

Mysql数据库中SELECT语句的WHERE条件中AND和OR之间的区别?

[英]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可以是jackjohn时返回记录,那么使用...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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM