SQL 将列组合成字符串
我有一个表,其中包含以下列:
id、t1、t2、t3、t4
均为位类型 (还有其他列,但我只显示相关的列)
现在,我需要根据 ID 获取以下字符串:
t1 t2 t3 t4
我想到的最好的就是这个:
declare @t1 bit, @t2 bit...
Select @t1 = t1, @t2 = t2 from t where id = 1
declare @theString
set @theString = ''
if @t1 = 1
set @theString = @theString + 't1 '
if @t2 = 1
set @theString = @theString + 't2 '
...
有没有更好的方法来实现这个目标?请注意,我无法更改表格。这样的格式可能非常糟糕。
I have a table with the following columns in table:
id, t1, t2, t3, t4
All are of type bit
(There are other columns as well, however I am only showing the relevant one)
Now, I need to get the following string based on the ID:
t1 t2 t3 t4
The best I thought of would be this:
declare @t1 bit, @t2 bit...
Select @t1 = t1, @t2 = t2 from t where id = 1
declare @theString
set @theString = ''
if @t1 = 1
set @theString = @theString + 't1 '
if @t2 = 1
set @theString = @theString + 't2 '
...
Is there a better way to achieve this? Please note that I can not change the table. Its probably very bad formatted like that.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为这可以做到:
可能需要一些调整,具体取决于您使用的数据库系统/语言。
I think this would do it:
Might take a bit of tweaking, depending on what database system/language you are using.