2

有没有办法在mysql中编写递归查询。相当于 oracle 中的 connect by (level or Prior)。我搜索了 google 和 stackoverflow,没有直接的等价物。但是有什么办法可以得到它。

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

输入:

recursive

输出:

recusiv
4

1 回答 1

3

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

使用的功能:

现场演示: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
于 2013-08-29T09:33:48.633 回答