如何使用Cross应用SQL中的单独行分开一个以上的逗号分隔列
I have one table having following rows
Name | Phones | Courses |
---|---|---|
ABC | 123, 456 | HTML, Java |
XYZ | 321, 654 | PHP, CSS |
now I want to write a SELECT query to get these comma separated Phone and Courses as a separate row.通过使用此选项查询,我会产生结果,直到电话列,但粘在第二列。 Select query is
SELECT
Name,
value phone,
courses
FROM
tblName
CROSS APPLY STRING_SPLIT(phones, ',');
This query shows me following results:
Name | Phones | Courses |
---|---|---|
ABC | 123 | HTML, Java |
ABC | 456 | HTML, Java |
XYZ | 321 | PHP, CSS |
XYZ | 654 | PHP, CSS |
Please help me to split Courses column as like Phones and want to yield following results:
Name | Phones | Courses |
---|---|---|
ABC | 123 | HTML |
ABC | 456 | HTML |
ABC | 123 | Java |
ABC | 456 | Java |
XYZ | 321 | PHP |
XYZ | 654 | PHP |
XYZ | 321 | CSS |
XYZ | 654 | CSS |
I have one table having following rows
Name | Phones | Courses |
---|---|---|
ABC | 123, 456 | HTML, Java |
XYZ | 321, 654 | PHP, CSS |
now I want to write a SELECT query to get these comma separated Phone and Courses as a separate row. By using this SELECT query I yield results till Phone column but stuck at 2nd column. Select query is
SELECT
Name,
value phone,
courses
FROM
tblName
CROSS APPLY STRING_SPLIT(phones, ',');
This query shows me following results:
Name | Phones | Courses |
---|---|---|
ABC | 123 | HTML, Java |
ABC | 456 | HTML, Java |
XYZ | 321 | PHP, CSS |
XYZ | 654 | PHP, CSS |
Please help me to split Courses column as like Phones and want to yield following results:
Name | Phones | Courses |
---|---|---|
ABC | 123 | HTML |
ABC | 456 | HTML |
ABC | 123 | Java |
ABC | 456 | Java |
XYZ | 321 | PHP |
XYZ | 654 | PHP |
XYZ | 321 | CSS |
XYZ | 654 | CSS |
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
由于 - 根据您的描述 - 您使用
交叉应用
,并且您的查询已成功执行,因此这意味着您使用的是SQL Server DB,而不是我的SQL。您可以进行两个交叉应用
以获得预期的结果。 This will produce exactly the outcome you have shown in your question:You can verify this here: db< >fiddle
But this is very risky and you really should avoid such comma-separated contents in one column.我强烈建议将来为不同值创建单独的列。
Since - according to your description - you used
CROSS APPLY
and your query was successfully executed, this means you are using a SQL Server DB, not MY SQL. You can do twoCROSS APPLY
to get your expected result. This will produce exactly the outcome you have shown in your question:You can verify this here: db<>fiddle
But this is very risky and you really should avoid such comma-separated contents in one column. I highly recommend to create separate columns for the different values in future.
好吧,我自己找到了解决方案。
here is the working query:
Thanks everyone.
Well I found the solution myself.
here is the working query:
Thanks everyone.