如何在mysql查询中使用2个条件?

时间:2022-03-10 15:39:57

Can anyone tell me what is the wrong with this query ? This code is not working:

谁能告诉我这个查询有什么问题?此代码无效:

SELECT index_no FROM test_record_tbl WHERE year='2012' and year='2013';

This code is working:

这段代码有效:

SELECT index_no FROM test_record_tbl WHERE year='2012' or year='2013';

3 个解决方案

#1


2  

If I understand you correctly, you want to get index_no which record belongs on year 2012 and 2013.

如果我理解正确,您希望获得index_no哪个记录属于2012年和2013年。

This problem is often called Relational Division.

这个问题通常被称为关系部门。

SELECT  index_no 
FROM    test_record_tbl 
WHERE   year IN ('2012', '2013')
GROUP   BY index_no
HAVING  COUNT(DISTINCT year) = 2

#2


3  

You're failing because the year can (presumably) never be both 2012 AND 2013.

你失败了,因为这一年(可能)永远不会是2012年和2013年。

And is an exclusive operator, meaning that both conditions MUST BE TRUE for the statement to evaluate as true.

并且是一个独占运算符,这意味着两个条件必须是正确的,以使语句评估为真。

OR means one, OR the other, OR both

OR表示一个,或另一个,或两者

#3


0  

There is a difference between and and or.

和和之间有区别。

You can never have year 2012 and year 2013 at the same time, but a year can fall either in year 2012 or year 2013.

您永远不会同时拥有2012年和2013年,但2012年或2013年可能会下降一年。

That's why your first SQL query brought no result, while the second did.

这就是为什么你的第一个SQL查询没有结果,而第二个SQL查询没有结果。

#1


2  

If I understand you correctly, you want to get index_no which record belongs on year 2012 and 2013.

如果我理解正确,您希望获得index_no哪个记录属于2012年和2013年。

This problem is often called Relational Division.

这个问题通常被称为关系部门。

SELECT  index_no 
FROM    test_record_tbl 
WHERE   year IN ('2012', '2013')
GROUP   BY index_no
HAVING  COUNT(DISTINCT year) = 2

#2


3  

You're failing because the year can (presumably) never be both 2012 AND 2013.

你失败了,因为这一年(可能)永远不会是2012年和2013年。

And is an exclusive operator, meaning that both conditions MUST BE TRUE for the statement to evaluate as true.

并且是一个独占运算符,这意味着两个条件必须是正确的,以使语句评估为真。

OR means one, OR the other, OR both

OR表示一个,或另一个,或两者

#3


0  

There is a difference between and and or.

和和之间有区别。

You can never have year 2012 and year 2013 at the same time, but a year can fall either in year 2012 or year 2013.

您永远不会同时拥有2012年和2013年,但2012年或2013年可能会下降一年。

That's why your first SQL query brought no result, while the second did.

这就是为什么你的第一个SQL查询没有结果,而第二个SQL查询没有结果。