简体   繁体   中英

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; only field 1 is what I want, so do I need to include field 2 and field 3 too in SELECT as in 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.

As with WHERE clause, you can also have multiple check with either OR and/or AND to merge them. 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; section:

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?

If field1 is all you need, you can safely ignore mentioning field2, field3, etc in WHERE clause.

Take a look at this MySQL SELECT documentation for more info.

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. SELECT gives you the output and WHERE filters out the data to get to the point outputs

You can use both Both are correct. if you want to use one or more condition in the same query use And the condition

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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