用于检查名称是否以元音开头和结尾的SQL查询

时间:2016-04-14 15:29:19

标签: mysql sql select

我想查询表CITY中的STATION(id, city, longitude, latitude)名称列表,这些名称的元音是他们的第一个和最后一个字符。结果不能包含重复项。

为此,我写了一个像WHERE NAME LIKE 'a%'这样的查询,它有25个条件,每个元音都用于其他每个元音,这是非常笨拙的。有没有更好的方法呢?

27 个答案:

答案 0 :(得分:47)

您可以使用regular expression

<form id="form" method="post">
<input class="oomText" type="text" name="accountName" placeholder="Account Name"><br>
<input class="oomButton submit" type="submit" name="submit" value="submit">

<script>
$(document).ready(function(){
  
$('#form').on('submit', function(e){
  
    var clickBtnValue = $('.oomButton .submit').val();
    var ajaxurl = 'http://clients.oomdo.com/provision/ajax.php',
    data =  {'action': clickBtnValue};
    $.post(ajaxurl, data, function (response) {
        alert("Account Created");
    });
});

});
</script>
</form>

答案 1 :(得分:12)

Microsoft SQL服务器中,您可以从以下查询中实现此目的:

SELECT distinct City FROM STATION WHERE City LIKE '[AEIOU]%[AEIOU]'

SELECT distinct City FROM STATION WHERE City LIKE '[A,E,I,O,U]%[A,E,I,O,U]'

答案 2 :(得分:11)

使用正则表达式。

WHERE name REGEXP '^[aeiou].*[aeiou]$'

^$将匹配锚定到值的开头和结尾。

在我的测试中,这不会在name列上使用索引,因此需要执行完整扫描,

WHERE name LIKE 'a%a' OR name LIKE 'a%e' ...

我认为要使用一个索引,你需要使用每个测试第一个字母的查询联合。

SELECT * FROM table
WHERE name LIKE 'a%' AND name REGEXP '[aeiou]$'
UNION
SELECT * FROM table
WHERE name LIKE 'e%' AND name REGEXP '[aeiou]$'
UNION
SELECT * FROM table
WHERE name LIKE 'i%' AND name REGEXP '[aeiou]$'
UNION
SELECT * FROM table
WHERE name LIKE 'o%' AND name REGEXP '[aeiou]$'
UNION
SELECT * FROM table
WHERE name LIKE 'u%' AND name REGEXP '[aeiou]$'

答案 3 :(得分:4)

你可以试试这个

    select city
    from station where SUBSTRING(city,1,1) in ('A','E','I','O','U') and 
    SUBSTRING(city,-1,1) in ('A','E','I','O','U');

答案 4 :(得分:4)

您可以尝试一个简单的MySQL解决方案:

SELECT DISTINCT city FROM station WHERE city REGEXP "^[aeiou].*";

答案 5 :(得分:4)

您可以对第一个和最后一个字符进行子字符串,并将其与IN关键字

进行比较
SELECT DISTINCT city
FROM   station
WHERE  city RLIKE '^[aeiouAEIOU].*[aeiouAEIOU]$'

答案 6 :(得分:3)

SELECT distinct CITY 
FROM STATION 
where (CITY LIKE 'a%' 
    OR CITY LIKE 'e%' 
    OR CITY LIKE 'i%' 
    OR CITY LIKE 'o%'
    OR CITY LIKE 'u%'
) AND (CITY LIKE '%a' 
    OR CITY LIKE '%e'
    OR CITY LIKE '%i'
    OR CITY LIKE '%o'
    OR CITY LIKE '%u'
)

答案 7 :(得分:3)

以下查询适用于Orale DB:

select distinct(city) from station where upper(substr(city, 1,1)) in ('A','E','I','O','U') and upper(substr(city, length(city),1)) in ('A','E','I','O','U');

答案 8 :(得分:2)

尝试以下方法:

select distinct city 
from station 
where city like '%[aeuio]'and city like '[aeuio]%' Order by City;

答案 9 :(得分:2)

下面的一个在MySQL中对我有用:

SELECT DISTINCT CITY FROM STATION WHERE SUBSTR(CITY,1,1) IN ('A','E','I','O','U') AND SUBSTR(CITY,-1,1) in ('A','E','I','O','U');

答案 10 :(得分:1)

对于MS访问或MYSQL服务器

SELECT city FROM station
WHERE City LIKE '[aeiou]%'and City LIKE '%[aeiou]';

答案 11 :(得分:1)

您还可以编写这样的硬代码,在每种情况下都进行检查,对于初学者来说很容易理解

SELECT DISTINCT CITY 
FROM STATION
WHERE CITY LIKE 'A%A' OR CITY LIKE 'E%E' OR CITY LIKE 'I%I' OR CITY LIKE 'O%O' OR
CITY LIKE 'U%U' OR CITY LIKE 'A%E' OR CITY LIKE 'A%I' OR CITY LIKE 'A%O' OR 
CITY LIKE 'A%U' OR CITY LIKE 'E%A' OR CITY LIKE 'E%I' OR CITY LIKE 'E%O' OR 
CITY LIKE 'E%U' OR CITY LIKE 'I%A' OR CITY LIKE 'I%E' OR CITY LIKE 'I%O' OR 
CITY LIKE 'I%U' OR CITY LIKE 'O%A' OR CITY LIKE 'O%E' OR CITY LIKE 'O%I' OR 
CITY LIKE 'O%U' OR CITY LIKE 'U%A' OR CITY LIKE 'U%E' OR CITY LIKE 'U%I' OR 
CITY LIKE 'U%O'

答案 12 :(得分:1)

尝试以下代码,

SELECT DISTINCT CITY
FROM   STATIOn
WHERE  city RLIKE '^[aeiouAEIOU].*.[aeiouAEIOU]$'

答案 13 :(得分:0)

对于oracle:

-Xlog-implicits

答案 14 :(得分:0)

我希望这会有所帮助

select distinct city from station where lower(substring(city,1,1)) in ('a','e','i','o','u') and lower(substring(city,length(city),length(city))) in ('a','e','i','o','u') ;

答案 15 :(得分:0)

在 MS SQL 中使用简单的左、右函数为我工作

select city from station where left(city,1) in ('a','e','i','o','u') and right(city,1) in ('a','e','i','o','u')

答案 16 :(得分:0)

select distinct(city) from STATION 
where lower(substr(city, -1)) in ('a','e','i','o','u') 
      and lower(substr(city, 1,1)) in ('a','e','i','o','u');

答案 17 :(得分:0)

我的简单解决方案:::

选择不同的城市

从站

在哪个城市喜欢'[a,e,i,o,u]%[a,e,i,o,u]';

答案 18 :(得分:0)

您可以使用以下正则表达式 并反转结果

^[^aeiou]|[^aeiou]$

即使输入包含单个字符,此方法也有效。它应该可以在不同的正则表达式引擎中使用。

MySQL

SELECT city
FROM (
    SELECT 'xx' AS city UNION
    SELECT 'ax'         UNION
    SELECT 'xa'         UNION
    SELECT 'aa'         UNION
    SELECT 'x'          UNION
    SELECT 'a'
) AS station
WHERE NOT city REGEXP '^[^aeiou]|[^aeiou]$'

PostgreSQL

WHERE NOT city ~ '^[^aeiou]|[^aeiou]$'

Oracle

WHERE NOT REGEXP_LIKE(city, '^[^aeiou]|[^aeiou]$')`

SQL Server

不支持正则表达式。将LIKE子句与方括号一起使用:

WHERE city LIKE '[aeiou]%' AND city LIKE '%[aeiou]'

答案 19 :(得分:0)

您可以使用LEFT()RIGHT()函数。 Left(CITY,1)将从左侧获得CITY的第一个字符。 Right(CITY,1)将从右开始获得CITY的第一个字符(CITY的最后一个字符)。

DISTINCT用于删除重复项。为了使比较不区分大小写,我们将使用LOWER()函数。

SELECT DISTINCT CITY
FROM STATION
WHERE LOWER(LEFT(CITY,1)) IN ('a', 'e', 'i', 'o', 'u') AND
      LOWER(RIGHT(CITY,1)) IN ('a', 'e', 'i', 'o', 'u')

答案 20 :(得分:0)

以下两个语句都适用于Microsoft SQL SERVER

SELECT DISTINCT
    city
FROM
    station
WHERE
    SUBSTRING(lower(CITY), 1, 1) IN ('a', 'e', 'i', 'o', 'u')
    AND SUBSTRING(lower(CITY), LEN(CITY), 1) IN ('a', 'e', 'i', 'o', 'u');

SELECT DISTINCT
    City
FROM
    Station
WHERE
    City LIKE '[A, E, O, U, I]%[A, E, O, U, I]'
ORDER BY
    City;

答案 21 :(得分:0)

在Oracle中:

SELECT DISTINCT city 
FROM station 
WHERE SUBSTR(lower(CITY),1,1) IN ('a','e','i','o','u') AND SUBSTR(lower(CITY),-1) IN ('a','e','i','o','u');

答案 22 :(得分:0)

尝试以下方法:

select distinct city from station where city REGEXP '^[aeiou]' and city REGEXP '[aeiou]$';

答案 23 :(得分:0)

以元音

开头尝试此操作

甲骨文:

select distinct *field* from *tablename* where SUBSTR(*sort field*,1,1) IN('A','E','I','O','U') Order by *Sort Field*;

答案 24 :(得分:0)

在MSSQL中,这可能是这样的:

transpiled

答案 25 :(得分:0)

SELECT DISTINCT city
FROM   station
WHERE  city RLIKE '^[^aeiouAEIOU]'OR city RLIKE'[^aeiouAEIOU]$'

答案 26 :(得分:-2)

Select distinct(city) from station where city RLIKE '^[aeiouAEIOU]'

非常简单的答案。