简体   繁体   English

我可以为 1 个 SELECT (mysql) 做多个 WHERE 吗?

[英]Can I do multiple WHERE for 1 SELECT (mysql)?

I don't know if I am stupid or not but I'm not sure if I can do我不知道我是否愚蠢,但我不确定我是否能做到

SELECT field 1 FROM table WHERE field 1=value 1 AND field 2=value 2 AND field 3=value 3

or或者

SELECT field 1, field 2, field 3 FROM table WHERE field 1=value 1 AND field 2=value 2 AND field 3=value 3

I don't know if this stupid or not or maybe there a risk like result not found or error or something in future if I use the first one, I don't know.我不知道这是否愚蠢,或者如果我使用第一个,我不知道将来是否存在找不到结果或错误之类的风险。

I tried the first one and it worked but I afraid if something will happen in future.我尝试了第一个并且它有效,但我担心将来会发生什么事情。

I did google but can't find anything, maybe because my keyword lol.我做了谷歌,但找不到任何东西,可能是因为我的关键字哈哈。

Tldr; TLDR; only field 1 is what I want, so do I need to include field 2 and field 3 too in SELECT as in WHERE?只有字段 1 是我想要的,所以我需要在 SELECT 中包括字段 2 和字段 3,就像在 WHERE 中一样?

Also I am a beginner and new here, and english is not my native language, so I'm sorry if this look rude to you and I don't meant to.另外我是初学者和新手,英语不是我的母语,所以如果这对你来说很粗鲁,我很抱歉,我不是故意的。 Thanks!谢谢!

You can do both query and it will work fine.您可以同时进行查询,它会正常工作。

SELECT clause can accept various fields as needed. SELECT 子句可以根据需要接受各种字段。

As with WHERE clause, you can also have multiple check with either OR and/or AND to merge them.与 WHERE 子句一样,您也可以使用OR和/或AND进行多次检查以合并它们。 It all depends on your requirement.这一切都取决于您的要求。

For example, you can do these and it will work fine:例如,您可以执行以下操作,它会正常工作:

  • SELECT field1 WHERE field1 = value1 OR (field2 = value2 AND field3 = value3)
  • SELECT field2 WHERE field2 = value2 AND field3 = value3

As for the TLDR;至于TLDR; section:部分:

Tldr; TLDR; only field 1 is what I want, so do I need to include field 2 and field 3 too in SELECT as in WHERE?只有字段 1 是我想要的,所以我需要在 SELECT 中包括字段 2 和字段 3,就像在 WHERE 中一样?

If field1 is all you need, you can safely ignore mentioning field2, field3, etc in WHERE clause.如果您只需要 field1,您可以放心地忽略在 WHERE 子句中提及 field2、field3 等。

Take a look at this MySQL SELECT documentation for more info.查看此MySQL SELECT 文档了解更多信息。

Both of them are correct!两个都是对的!

if you want to filter out by more attributes then you can add WHERE as much as you need, it has nothing to do with SELECT attributes.如果您想通过更多属性过滤掉,那么您可以根据需要添加WHERE ,它与SELECT属性无关。 SELECT gives you the output and WHERE filters out the data to get to the point outputs SELECT为您提供 output 和WHERE过滤掉数据以获得点输出

You can use both Both are correct.你可以同时使用两者都是正确的。 if you want to use one or more condition in the same query use And the condition如果您想在同一查询中使用一个或多个条件,请使用And the condition

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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