简体   繁体   English

SQL WHERE和LIKE子句重复

[英]SQL WHERE and LIKE clause duplicated

Hi guys I'm new to SQL and trying to learn it by myself. 嗨,大家好,我是SQL的新手,正在尝试自己学习。 I have the following questions when I did the tutorial questions in sqlzoo website. 在sqlzoo网站上进行教程问题时,我遇到以下问题。 Assume I have a table which contains info for all countries in the world. 假设我有一个表,其中包含世界上所有国家的信息。

When I want to query the country which its name containing 'A' and 'B' , I use the following query 当我想查询名称包含“ A”和“ B”的国家时,我使用以下查询

SELECT name 
FROM world
WHERE name LIKE '%A%'
AND name LIKE '%B%'

here I can repeated use the name LIKE query 在这里我可以重复使用name LIKE查询

However, when I want to query the country which its name is Vietnam and Thailand, I can only use the query 但是,当我要查询名称为越南和泰国的国家时,只能使用查询

SELECT name
FROM world 
WHERE name IN ('Thailand', 'Vietnam')

instead of 代替

WHERE name = 'Thailand'
AND name = 'Vietnam'

Can somebody kindly explain the reason behind this? 有人可以解释一下这背后的原因吗? When I can use many AND xxxx and when I cannot use that. 当我可以使用许多AND xxxx且无法使用时。

Thanks so much ! 非常感谢 !

WHERE name = 'Thailand'
AND name = 'Vietnam'

This one searches for the country which has both 'Thailand' and 'Vietnam' in its name. 这将搜索名称中同时具有“泰国”和“越南”的国家。 but

SELECT name
FROM world 
WHERE name IN ('Thailand', 'Vietnam')

This one searches Countries which names are either 'Thailand' OR 'Vietnam' 此搜索的国家名称为“泰国”或“越南”

That is the deference between them. 那是他们之间的尊重。

WHERE name = 'Thailand' AND name = 'Vietnam'

AND is a boolean AND and return the result where the country name = Thailand and name = Vietnam in a single row. AND是布尔值AND,并在单行中返回国家名称=泰国和名称=越南的结果。

Instead use 改为使用

WHERE name = 'Thailand' OR name = 'Vietnam'

And returns to you the results which obey ALL the rules you have stated. 并向您返回遵循您已陈述的所有规则的结果。

Can a country name be 'Thailand' AND the SAME country name be 'Vietnam', at the same time? 一个国家名称可以同时为“ Thailand”,而同一国家名称可以为“ Vietnam”吗? i hope you see that the answer to this is no. 我希望您看到答案是否定的。 So this is a classic example where you need to use 'OR' instead of 'AND', Why? 因此,这是一个经典示例,您需要使用“或”而不是“与”,为什么? because then you will get a result back where the name = 'Thailand' and also the result where the name = 'Vietnam', because both of them obey the 'OR' rule, meaning both names are Either 'Vietnam' or 'Thailand'. 因为这样您将返回名称='Thailand'的结果以及名称='Vietnam'的结果,因为它们都遵守'OR'规则,这意味着两个名称均为'Vietnam'或'Thailand' 。

The reason this wrong way of using AND instead of OR is working for you in the first case you mentioned is because you are using wildcards. 在您提到的第一种情况下,使用AND而不是OR的这种错误方式对您有效的原因是因为您使用通配符。 and there are country names where you can have an 'A' and a 'B' at the SAME TIME in different places inside the SAME country name. 并且有一些国家/地区名称,您可以在同一国家/地区的国家名称内的不同位置,在同一时间使用“ A”和“ B”。

Hope this helps. 希望这可以帮助。

WHERE name IN ('Thailand', 'Vietnam')

Is the same as 是相同的

WHERE name = 'Thailand'
OR name = 'Vietnam'

if you want to check condition to match any one element from the given list of element then you should use. 如果要检查条件以匹配给定元素列表中的任何一个元素,则应使用。

WHERE name IN ('Thailand', 'Vietnam')

and if you want to satisfy both condition then you should use below code 如果要同时满足这两个条件,则应使用以下代码

WHERE name = 'Thailand'
AND name = 'Vietnam'

Note :- result of AND condition should always false because name can be either 'Thailand' or 'Vietnam' 注意:-AND条件的结果应始终为false,因为名称可以是'Thailand'或'Vietnam'

WHERE conditions are always applied per record . 每个记录始终应用WHERE条件。 So 所以

WHERE name = 'Thailand'
AND name = 'Vietnam'

does not mean: find all records in the table for the names 'Thailand' and 'Vietnam'. 并不意味着:在表中找到名称为“ Thailand”和“ Vietnam”的所有记录。

It means: Find records that have a name that equals both 'Thailand' and 'Vietnam'. 这意味着:查找名称等于“ Thailand”和“ Vietnam”的记录。 But a name can only be either 'Thailand' or 'Vietnam' or none of them, never both at the same time of course. 但是,名称只能是“泰国”或“越南”,也不能全都用,当然不能同时使用。

So use OR instead in order to find records where the name either equals 'Thailand' or 'Vietnam': 因此,请改用OR来查找名称等于“ Thailand” “ Vietnam”的记录:

WHERE name = 'Thailand'
OR name = 'Vietnam'

And as you already know you can look up the list of countries instead with IN which makes it more readable 如您所知,您可以使用IN来查找国家/地区列表,从而使其更具可读性

WHERE name IN ('Thailand', 'Vietnam')

and less prone to errors. 而且不易出错。 A typical mistake: 一个典型的错误:

WHERE name = 'Thailand' OR name = 'Vietnam'
AND size = 'big'

which translates to 转化为

WHERE name = 'Thailand' 
OR (name = 'Vietnam' AND size = 'big')

rather than 而不是

WHERE (name = 'Thailand' OR name = 'Vietnam')
AND size = 'big'

because AND has precedence over OR . 因为AND优先于OR So when using both AND and OR use parentheses. 因此,当同时使用ANDOR使用括号。 And as mentioned use IN instead of OR when possible. 如前所述,请尽可能使用IN代替OR

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

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