Mysql Query查找字符串中的不同字符

时间:2013-08-29 07:27:03

标签: mysql sql database recursive-query

有没有办法在mysql中编写递归查询。在oracle中通过(level或Prior)连接的等价物。我搜索谷歌以及stackoverflow,没有直接的等价。但是有任何工作可以解决它。

我有一个字符串,我必须遍历字符串中的单个字符并仅打印字符串的不同字符。

输入:

recursive

输出:

recusiv

1 个答案:

答案 0 :(得分:2)

我想出了这个“简单”的解决方案。

使用的功能:

现场演示:http://www.sqlfiddle.com/#!2/d41d8/19186

SELECT
  GROUP_CONCAT( chars.c SEPARATOR '') AS allchars
FROM (

  SELECT DISTINCT 
    SUBSTRING( str.str, pos.pos, 1 ) AS c

  FROM 

  ( SELECT x1.x + x2.x*10 AS pos
    FROM
    ( SELECT 0 AS x UNION ALL
      SELECT 1 UNION ALL
      SELECT 2 UNION ALL
      SELECT 3 UNION ALL
      SELECT 4 UNION ALL
      SELECT 5 UNION ALL
      SELECT 6 UNION ALL
      SELECT 7 UNION ALL
      SELECT 8 UNION ALL
      SELECT 9
    ) AS x1
    INNER JOIN
    ( SELECT 0 AS x UNION ALL
      SELECT 1 UNION ALL
      SELECT 2 UNION ALL
      SELECT 3 UNION ALL
      SELECT 4 UNION ALL
      SELECT 5 UNION ALL
      SELECT 6 UNION ALL
      SELECT 7 UNION ALL
      SELECT 8 UNION ALL
      SELECT 9
    ) AS x2
  ) AS pos

  INNER JOIN

  ( SELECT 'recursive' AS str UNION ALL
    SELECT 'XYZ'
  ) AS str

) AS chars