substring_index函数不存在错误

时间:2012-09-24 12:52:51

标签: mysql rows

我有一个名为'data'的表,用于存储电子邮件地址和用户城市,我想找到最受欢迎的电子邮件域。我使用以下查询来查找具有最大值的行。 我的桌子就是这样的:

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)

但我不想使用限制,因为它不会返回多行。所以我使用此answer

使用了以下查询
    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不存在”。帮助我。

1 个答案:

答案 0 :(得分: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