如何在数据库中查找值并进行相应显示 [英] how to find a value in datatabase and display accordingly
本文介绍了如何在数据库中查找值并进行相应显示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
嗨
我有一个包含5列的数据表,我必须检查数据表中的ID字段是否重复,如果我必须找到列名的值并将其附加在第一行中,这样就可以避免多行
ID名称年龄
1萨夫夫2
1萨德夫4
我必须像这样显示
ID名称年龄
1 sadf 2,4
Hi
I have a datatable with 5 columns i have to check whether the ID field in datatable is duplicate if so i have to find the value of column name and append it in the first row so tht multiple rows can be avoided
ID Name Age
1 sadf 2
1 sadf 4
i have to show like this
ID Name Age
1 sadf 2,4
推荐答案
这不是一个好的设计,它使查询数据变得困难,并且破坏了关系数据库的目的.
This is not a good design and makes querying data difficult and defeats the purpose of a relational database.
嘿,
作为开发人员,您必须改善自己的Google艺术:)查看以下解决方案,它将对您有所帮助.
经过修改的解决方案,来自: T-SQL的通用解决方案 [ ^ ]
Hey there,
As a developer, you MUST improve your Google-arts :) Check out the below solution, it should help you.
A modified solution from: Common Solutions for T-SQL[^]
SELECT
YT.ID,
AgeList = substring((SELECT ( ', ' + RTRIM(LTRIM(STR(YT2.ItemId))))
FROM YourTable YT2
WHERE YT.ID = YT2.ID
ORDER BY
YT2.ID,
YT2.Age
FOR XML PATH( '' )
), 3, 5000)FROM YourTable YT
希望对您有所帮助
Hope this helps, regards
参见类似的讨论此处 [ ^ ].可能对您有用.
Hi,
See the similar discussion Here[^]. It may work for you.
这篇关于如何在数据库中查找值并进行相应显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文