0

我有一个名为“数据”的表,其中存储了用户的电子邮件地址和城市,我想找到最受欢迎的电子邮件域。我使用以下查询来查找具有最大值的行。我的桌子的例子:

Email           Name        City
dsmks@gmail.com     John    California
sak@gmail.com       Leo sydney
dsnk@gmail.com      Ross    NY
askd@yahoo.com      Ronny   Canberra
ajs@yahoo.com       Monty   London
kasl@yahoo.com      Jim washington
uy@hotmail.com      Finn    Las vegas

我已经使用这个查询计算了答案

  select  x.city, x.No_of_People from (select e.city, count(e.city) as No_of_People from data e group by  e.city) x
where  x.No_of_People = (select max(x2.No_of_People) from (select e2.city, count(e2.city) as No_of_People from data e2 group by         e2.city) x2)

但我不想使用限制,因为它不会返回多行。所以我使用这个答案使用了以下查询

    select
  x.substring_index (email,'@',-1),
  x.No_of_Users
from
  (select
    e.substring_index (email,'@',-1), 
    count(e.substring_index (email,'@',-1)) as No_of_Users
  from
    data e
  group by 
    e.substring_index (email,'@',-1)) x
where
  x.No_of_Users = 
    (select
      max(x2.No_of_Users)
    from
      (select
         e2.substring_index (email,'@',-1),
         count(e2.substring_index (email,'@',-1)) as No_of_Users
      from
         data e2
      group by 
        e2.substring_index (email,'@',-1)) x2)

我正在使用的查询给出了这个错误“FUNCTION e2.substring_index 不存在”。帮我。

4

1 回答 1

3

使用该函数的语法错误。别名出现在列上,而不是函数上。例如,您的最后一个子查询应使用以下语法:

  from (select substring_index(e2.email,'@',-1) as strind,
               count(substring_index(e2.email,'@',-1)) as No_of_Users
        from data e2
        group by substring_index (e2.email,'@',-1)
       ) x2

我还命名了第一列,因此您可以根据需要在子查询之外引用它。

要计算字符串中出现的次数,请使用以下技巧:

(length(e2.email) - length(replace(e2.email, '@', '')) as numAmpersands
于 2012-09-24T13:16:17.097 回答