假设我们有下表。

用户名|信息
------- | -------------
1 |大家好
2 |大家好



我需要分别输入所有{嗨,我今天有一个好}字。
有什么办法吗?如果要从整个数据库表中导出单词怎么办?

类似的结果也会很好。

最佳答案

试试这个:在Sql Server 2005或更高版本中

create table yourtable(RowID int, Layout varchar(200))
INSERT yourtable VALUES (1,'hello,world,welcome,to,tsql')
INSERT yourtable VALUES (2,'welcome,to,stackoverflow')


;WITH SplitSting AS
(
    SELECT
        RowID,LEFT(Layout,CHARINDEX(',',Layout)-1) AS Part
            ,RIGHT(Layout,LEN(Layout)-CHARINDEX(',',Layout)) AS Remainder
        FROM YourTable
        WHERE Layout IS NOT NULL AND CHARINDEX(',',Layout)>0
    UNION ALL
    SELECT
        RowID,LEFT(Remainder,CHARINDEX(',',Remainder)-1)
            ,RIGHT(Remainder,LEN(Remainder)-CHARINDEX(',',Remainder))
        FROM SplitSting
        WHERE Remainder IS NOT NULL AND CHARINDEX(',',Remainder)>0
    UNION ALL
    SELECT
        RowID,Remainder,null
        FROM SplitSting
        WHERE Remainder IS NOT NULL AND CHARINDEX(',',Remainder)=0
)
SELECT part FROM SplitSting ORDER BY RowID


SQLFIDDLE DEMO

关于mysql - 如何从SQL表或SQL数据库导出所有单词,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/13117710/

10-12 18:27