使用 SQL2000 将字段中的数据拆分为行
请帮我找到解决方案。我的表中有数据,就像
ID Code
1 123,456,789,12
2 456,073
3 69,76,56
我需要在行中列出代码
ID Code Ref
1 123,456,789,12 123
1 123,456,789,12 456
1 123,456,789,12 789
1 123,456,789,12 12
2 456,073 456
2 456,073 073
3 69,76,56 69
3 69,76,56 76
3 69,76,56 56
一样,如何在查询命令中执行此操作?我将使用 ref 列中的值来连接另一个表中的另一列。 感谢支持
Please help me to find a solution. I have data in table like
ID Code
1 123,456,789,12
2 456,073
3 69,76,56
I need to list of code in row
ID Code Ref
1 123,456,789,12 123
1 123,456,789,12 456
1 123,456,789,12 789
1 123,456,789,12 12
2 456,073 456
2 456,073 073
3 69,76,56 69
3 69,76,56 76
3 69,76,56 56
How do I do this in a query command? I'll be using the value in ref column to join another column in another tables.
Thanks for supports
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我的第一个建议是标准化你的数据库。一列应该包含一条信息。您的逗号分隔值违反了此规则,这就是您面临如此困难的原因。但由于人们很少采纳这个建议,这里有一个可能对你有用的拼凑。由于您要将其连接到另一个表,因此您实际上不需要将其自己的列中的每个值分开,您只需要能够在您的列中找到匹配的值:
这依赖于您的列中的代码采用精确的格式,以逗号分隔,不带空格。如果情况并非如此,那么这可能不会返回您想要获得的结果。
My first advice is to normalize your database. A column should contain a single piece of information. Your comma-delimited values violates this rule, which is why you're facing such difficulty. Since people seldom ever take that advice though, here's a kludge which might work for you. Since you're joining this to another table, you don't really need to separate out each value in its own column, you just need to be able to find a matching value in your column:
This relies on the codes in your column to be in an exact format, comma-delimited with no spaces. If that's not the case then this will likely not return what you're trying to get.
答案是标准化您的数据库。
同时,在大型集上表现更好的一种解决方法是使用临时表。 (
LIKE
搜索不能使用索引)此方法还显示了标准化数据和处理空格的一些步骤。
首先创建一个“Tally Table”(如果没有) 这是一次性交易,Tally 表对于 全部 种类 事物。
现在假设您的表是:
那么临时表是:
然后搜索是:
结果是:
The answer is to normalize your database.
In the meantime, a workaround that will perform better on large sets, is to use a temp table. (
LIKE
searches can't use an index)This approach also shows some steps towards normalizing the data and handles whitespace.
First create a "Tally Table" if you don't have one. This is a one-time deal, and Tally tables come in handy for all kinds of things.
Now suppose your tables are:
Then the temp table is:
Then the search is:
And the results are: