0

我想从 SQL 查询中提取表名。

SELECT col1, col2, count(1) as count_all,     FROM     tbl1, tbl2 where condition order by column

我要结果["tbl1", "tbl2"]

不必查询多个表。在这种情况下,查询将是

SELECT col1, col2, count(1) as count_all,     FROM     tbl1  where condition order by column

和预期的结果["tbl1"]

提前致谢!

4

1 回答 1

2

请注意,这可能会匹配 SQL 字符串中的内容,因此并不完美。

test = [
"SELECT col1, col2, count(1) as count_all FROM tbl1, tbl2 where condition order by column",
"SELECT col1, col2, count(1) as count_all FROM tbl1 where condition order by column",
"SELECT col1, col2, count(1) as count_all FROM tbl1",
]
tests.map { |str| str.match(/\s*from\s*([a-z_0-9]+(?:,\s*[a-z_0-9]+)*)\b/i); $1 }
#=> ["tbl1, tbl2", "tbl1", "tbl1"]
于 2012-07-20T11:09:56.270 回答