我如何从sqlserver获取 [英] How do I get from sqlserver

查看:97
本文介绍了我如何从sqlserver获取的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



在sql server中我有两个表。一个问题表和另一个选项表。两个表都有questionId。基于questionid获取特定问题的选项。如果我使用连接查询我没有得到预期的结果。我希望结果像第一行包含第一个选项的问题,其余的行只是optioons。然后接下来的问题,我怎样才能做到这一点?我需要通过api调用这个然后返回MVC视图。



我尝试过:



我写了查询并更改了它。但我没有得到预期的结果。

Hi,
In sql server i have two tables. One table for questions and another table for options. Both tables are having questionId. Based on questionid get the options for the particular question. If i use join query i dont get the expected result. I want result like the first row contains question with first option, the remaining rows are only optioons. Then next question, how can i achieve this? I need to call this through api then return to MVC views.

What I have tried:

I write the query and changed it. But i am not get the expected result.

推荐答案

如果我理解你的问题,你会想要一个输出:

If I understand your question correctly, you would like an output like:
Question 1   Option 1
             Option 2
             Option 3
Question 2   Option 1
             Option 2
             Option 3
             Option 4
...



如果是这种情况,那么我相信你在错误的层中进行格式化。 SQL不是为了格式化数据而设计的,只是为了获取和操作它。因此,如果您想进行格式化,则应使用专为此设计的客户端工具或例如SQL Server中的报告服务。



如果说的话,如果你真的想要消除重复值,你可以使用LAG / LEAD实现这一点。看看如何从中获取数据结果集中的上一行或下一行 [ ^ ]



增加:



如果我理解了这个要求,请看如何格式化SQL结果集中的重复值 [ ^ ]


这篇关于我如何从sqlserver获取的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆