SQL查询城市名称以元音开头和结尾

时间:2017-07-06 18:38:57

标签: sql

我写了这段代码(db2)并且它工作正常,但我想知道,有没有更短的方法来写这个?

Select Distinct city
From   station
Where  city Like 'A%a'
       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%e'
       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%i'
       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%o'
       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'
       Or city Like 'U%u';

11 个答案:

答案 0 :(得分:3)

我不是DB2专家,但这应该是相当便携的:

{
    from: 0,
    size: 10,
    query: {
        function_score: {
            query: { "match_all": {} },
            boost: "5",
            functions: [{
                filter: {
                    multi_match: {
                        query: query,
                        type: 'phrase_prefix',
                        fields: ['text', 'fullText']
                    }
                }
            }, {
                filter: {
                    multi_match: {
                        query: query,
                        fuzziness: "AUTO",
                        fields: ['text', 'fullText']
                    }
                }
            }],
            query: {},
            field_value_factor: {
                field: 'score',
                factor: 1.2,
                missing: 1
            }
        }
    },
    filter: {
        term: {
            enabled: true
        }
    }
}

您可能希望将其全部标准化为大写,以避免因某些原因以小写字母开头或以大写字母结尾的城市出现问题。

WHERE LEFT(city,1) IN ('A', 'E', 'I', 'O', 'U')
  AND RIGHT(city,1) IN ('a', 'e', 'i', 'o', 'u')

答案 1 :(得分:0)

我不是DB2的专家,但我认为你可以在WHERE LIKE中使用正则表达式。

检查文档: irradiance.total_irrad

Example:
Select Distinct city
From   station
Where  REGEXP_LIKE(city,'[aeiou].*[aeiou]')

答案 2 :(得分:0)

在LIKE子句中使用正则表达式。请查看此处的示例Using RegEx in SQL Server

在Sql server中,您的LIKE子句可能如下所示。

LIKE' [AEIOU]%[aeiou]'

在DB2中,语法略有不同。在这个页面上查看几个例子。

https://www.ibm.com/developerworks/data/library/techarticle/0301stolze/0301stolze.html

答案 3 :(得分:0)

您可以像这样使用substr

SELECT DISTINCT CITY 
FROM STATION WHERE SUBSTR(CITY,1,1) IN('A','E','I','O','U','a','e','i','o','u') 
and SUBSTR(CITY,-1,1) IN('A','E','I','O','U','a','e','i','o','u');

答案 4 :(得分:0)

从STEX REGEXP_LIKE(CITY,' ^ [^ aeiouAEIOU]。*');

选择DISTINCT CITY

答案 5 :(得分:0)

尝试以下操作:(oracle) 从REGEXP_LIKE(CITY,'^ [^ aeiouAEIOU]。*');的车站中选择不同的城市。

答案 6 :(得分:0)

您可以在 MySQL 中使用REGEXP来操作 正则表达式

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

针对不熟悉正则表达式的人

^[aeiou]    // Start with a vowel
.*          // Any characters at any times
[aeiou]$    // End with a vowel

答案 7 :(得分:0)

这将在SQL Server中用AND子句在3行中回答-

func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
    

        guard let windowScene = (scene as? UIWindowScene) else { return }
        window = UIWindow(frame: UIScreen.main.bounds)
        window?.windowScene = windowScene
        window?.rootViewController = MainNavigationController()
        window?.makeKeyAndVisible()
        
    }

...

此外,对于MySQL,您可以使用Regex-

SELECT DISTINCT CITY FROM *TableName* 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');

答案 8 :(得分:0)

select distinct city from station where REGEXP_LIKE(city,'[aeiou]$');

答案 9 :(得分:0)

对于Oracle,可以这样写-

SELECT * FROM(
    SELECT UNIQUE(city) FROM station WHERE
    LOWER(SUBSTR(city,1,1)) IN ('a','e','i','o','u')
    INTERSECT  
    SELECT UNIQUE(city) FROM station WHERE
    LOWER(SUBSTR(city,LENGTH(city),1)) IN ('a','e','i','o','u')
);

答案 10 :(得分:-1)

作为初学者,我就是这样解决的:

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%';