sql select - 将列中的数据合并为一个并对其运行不同的数据
我有一张表:
id coach1 coach2 department date
1 1 0 txt1 8/11/2010
2 3 0 txt2 1/11/2010
3 1 2 txt1 8/11/2010
4 3 1 txt1 8/11/2010
我想要做的是从两位教练那里获取一列,忽略零并且不重复教练的数字,所以这个例子的输出将是:
coach department date
1 txt1 8/11/2010
2 txt1 8/11/2010
3 txt1 1/11/2010
所以我使用:
SELECT id,coach1 as coach,department,date FROM tblWinnings where coach1>0 UNION SELECT id,coach2 as coach,department,date FROM tblWinnings where coach2>0
但现在我需要它与教练栏不同,
我该怎么做?
谢谢!
i have a table:
id coach1 coach2 department date
1 1 0 txt1 8/11/2010
2 3 0 txt2 1/11/2010
3 1 2 txt1 8/11/2010
4 3 1 txt1 8/11/2010
what i want to do is to get one column from both coaches, ignore the zeros and witout repeating the numbers of the coaches so the output of this example will be:
coach department date
1 txt1 8/11/2010
2 txt1 8/11/2010
3 txt1 1/11/2010
so i used:
SELECT id,coach1 as coach,department,date FROM tblWinnings where coach1>0 UNION SELECT id,coach2 as coach,department,date FROM tblWinnings where coach2>0
but now i need to have it distinct by the coach column
how do i do that?
thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果您只想要不同的值,请不要使用 Union All (没有 All 子句的联合可以防止重复)
因为您不断更新您的问题:如果您想要其他字段中的值,您将遇到问题,因为根据您使用的不同,它们可能会有所不同。 (最新?最早?随机?)
If you only want the distinct values, don't use Union All (union without the All clause prevents duplicates)
Since you keep updating your question: You're going to run into a problem if you want the values from the other fields, since they could be different depending on the one you use. (Latest? Earliest? Random?)
我不记得访问的语法,但类似
I cannot remember the syntax for access but something like