在连接中插入,代码混淆

时间:2014-12-30 16:25:51

标签: mysql sql

此代码返回TEXT格式列中最常用的单词description,位于表格' messages`上。

但是我不能在WHERE代码中坚持这个:

messages.tag = 'HELLO'

我需要它做它已经做的事情,但是在这个WHERE我尝试了这个代码并且不确定:

SELECT message, count(message) as count

FROM (
  SELECT 
      messages.id, 
      SUBSTRING_INDEX(SUBSTRING_INDEX(messages.message, ' ', count.n), ' ', -1)  as message 
  FROM 
  (select (h*100+t*10+u+1) n from
    (select 0 h union select 1 union select 2 union select 3 union select 4 union
    select 5 union select 6 union select 7 union select 8 union select 9) A,
    (select 0 t union select 1 union select 2 union select 3 union select 4 union
    select 5 union select 6 union select 7 union select 8 union select 9) B,
    (select 0 u union select 1 union select 2 union select 3 union select 4 union
    select 5 union select 6 union select 7 union select 8 union select 9) C
  )  as count
  INNER JOIN messages
    ON CHAR_LENGTH(messages.message)-CHAR_LENGTH(REPLACE(messages.message, ' ',''))>=count.n-1 
   ORDER BY id, n
) x
WHERE LENGTH(message) >= 5
    AND messages.tag = 'HELLO'
GROUP BY message
ORDER BY count DESC
LIMIT 10

2 个答案:

答案 0 :(得分:1)

您必须将where子句移动到子查询中。外部查询中不知道messages别名:

FROM (SELECT messages.id, 
             SUBSTRING_INDEX(SUBSTRING_INDEX(messages.message, ' ', count.n), ' ', -1)  as message 
      FROM  (select (h*100+t*10+u+1) n
             from (select 0 h union select 1 union select 2 union select 3 union select 4 union
                   select 5 union select 6 union select 7 union select 8 union select 9) A,
                  (select 0 t union select 1 union select 2 union select 3 union select 4 union
                   select 5 union select 6 union select 7 union select 8 union select 9) B,
                  (select 0 u union select 1 union select 2 union select 3 union select 4 union
                   select 5 union select 6 union select 7 union select 8 union select 9) C
           )  count INNER JOIN
           messages
           ON CHAR_LENGTH(messages.message)-CHAR_LENGTH(REPLACE(messages.message, ' ','')) >= count.n-1 
      WHERE messages.tag = 'HELLO'
      ORDER BY id, n
) x

另一个条件保留在外部查询中。

答案 1 :(得分:0)

试试这个,

我认为messages.tag应该是,不等于'HELLO'

SELECT message, count(message) as count
FROM (
       SELECT 
          messages.id, 
          SUBSTRING_INDEX(SUBSTRING_INDEX(messages.message, ' ', count.n), ' ', -1)  as message 
       FROM 
        (
          select (h*100+t*10+u+1) n 
          from
          (
            select 0 h union select 1 union select 2 union select 3 union select 4 
            union
            select 5 union select 6 union select 7 union select 8 union select 9) A,
          (select 0 t union select 1 union select 2 union select 3 union select 4 
            union
          select 5 union select 6 union select 7 union select 8 union select 9) B,
          (select 0 u union select 1 union select 2 union select 3 union select 4 
          union
           select 5 union select 6 union select 7 union select 8 union select 9) C
          ) count
        INNER JOIN messages
        ON CHAR_LENGTH(messages.message)-CHAR_LENGTH(REPLACE(messages.message, ' ',''))>=count.n-1
        WHERE messages.tag like '%HELLO%'   
        ORDER BY id, n
      ) x
WHERE LENGTH(message) >= 5        
GROUP BY message
ORDER BY count DESC
LIMIT 10