本文介绍了带WHERE,AND或OR的SQL Select语句的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想用MySQL执行SELECT查询.我的目标是在兽医数据库中选择所有狗,它们分别是sex=malefur=short和(color=black or size=big)

I would like to perform a SELECT query with MySQL. My goal is to select all the dogs in a vet database that would be sex=male and fur=short and (color=black or size=big)

注意:我要选择黑色或大狗.他们不必满足2个要求.他们只需要实现其中一个即可.

Note: I want to select dogs that are either black or size is big. They don't have to fulfill the 2 requirements. They just need to fulfill either one.

我已经在下面编写了SQL语句,但是我不确定我是否正确:

I have written the SQL statement below but I'm not not sure if I'm right:

SELECT name, sex, fur, color
FROM dogs
WHERE TRUE sex='male' AND fur='short' AND color='black' OR size="big";

如果太混乱,请原谅我的措辞.

Pardon my phrasing if it's too confusing.

推荐答案

根据 MySQL的运算符优先级 AND的优先级高于OR.

所以C1 AND C2 OR C3将被视为(C1 AND C2) OR C3

要覆盖默认优先级,您需要使用括号:C1 AND (C2 OR C3)

To override the default precedence you need to use parenthesis as:C1 AND (C2 OR C3)

对于您而言,正确的查询是:

In your case the right query is:

SELECT name, sex, fur, color
FROM dogs
WHERE sex='male' AND fur='short' AND (color='black' OR size="big");

这篇关于带WHERE,AND或OR的SQL Select语句的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-25 03:54