使用 WHERE、AND、OR 的 SQL 选择语句

发布于 2024-09-16 12:23:08 字数 410 浏览 2 评论 0原文

我想使用 MySQL 执行 SELECT 查询。我的目标是选择兽医数据库中的所有狗,这些狗是 sex=malefur=short 和(color=black 或 size=big code>)

注意:我想选择黑色或体型较大的狗。他们不必满足这两个要求。他们只需要满足其中一个即可。

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

SELECT name, sex, fur, color 
FROM dogs 
WHERE TRUE sex='male' AND fur='short' AND 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)

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.

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

梦屿孤独相伴 2024-09-23 12:23:09

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

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

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

在您的情况下,正确的查询是:

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

According to Operator precedence for MySQL AND has higher precedence than OR.

So
C1 AND C2 OR C3 will be treated as (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");
北笙凉宸 2024-09-23 12:23:09

确保添加括号,以便正确评估 OR 条件。

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

Make sure to add parentheses, so the OR condition gets evaluated correctly.

SELECT name, sex, fur, color 
FROM dogs 
WHERE sex='male' AND fur='short' AND (color='black' OR size='big');
Smile简单爱 2024-09-23 12:23:09

您在描述目标时使用括号的方式是正确的。语法是:

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

The way you use parentheses in the description of your goal is correct. The syntax is:

SELECT name, sex, fur, color 
    FROM dogs 
    WHERE sex="male" AND fur="short" AND (color="black" OR size="big");
以为你会在 2024-09-23 12:23:09

我已经使用过这个及其工作原理;

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

I have used this and its working;

SELECT name, sex, fur, color 
FROM dogs 
WHERE sex="male" AND fur="short" AND (color="black" || size="big");
春风十里 2024-09-23 12:23:09

从狗中选择姓名、性别、皮毛、颜色,其中性别 = '雄性',皮毛 = '短' 且(颜色 = '黑色' 或尺寸 = '大');

select name, sex, fur,color from dogs where sex ='male' and fur = 'short' and (color ='black' or size ='big');

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文