简体   繁体   English

SQL查询以查找表中包含所有元音的所有值。

[英]SQL query to find all values in a table that contain all vowels.

This is the first week I have ever worked with SQL. 这是我使用SQL的第一周。 It's rather straightforward and has been fairly easy to pick up. 它非常简单,并且很容易拿起。

I had to write a query to find all countries that contain all the vowels ('aeiou') and no white spaces in their name. 我必须写一个查询来查找所有包含所有元音(aeiou)且名称中没有空格的国家。 The only country that fit this criteria was Mozambique. 符合这一条件的唯一国家是莫桑比克。

SELECT
  name 
FROM 
  world 
WHERE 
  name LIKE '%a%' AND name LIKE '%e%' AND name LIKE '%i%' AND name LIKE       
 '%o%' AND name LIKE '%u%' AND name NOT LIKE '% %'

The above query worked. 上面的查询有效。 I wanted to now if there was a way to shorten this, so I could use one LIKE statement and check if the word contained all of those letters in it's name without order mattering. 我现在想知道是否有一种方法可以缩短此时间,因此我可以使用一个LIKE语句并检查该单词是否包含其名称中的所有字母,而不必考虑顺序。

In PostgreSQL you can do that like so: 在PostgreSQL中,您可以这样做:

    SELECT w.name
    FROM world AS w
    WHERE w.name SIMILAR TO '%(a|e|i|o|u)%' 
      AND w.name NOT SIMILAR TO '% %';

There are other options but nothing much shorter. 还有其他选择,但没有什么要简短的了。 A couple examples below. 下面是几个例子。

...WHERE LOCATE('a',name) AND LOCATE('e',name) ...

...WHERE name REGEXP 'a' AND name REGEXP 'e' ...

( mysql 5.7 pattern matching documentation may help too) mysql 5.7模式匹配文档也可能有帮助)

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

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