sql select - 将列中的数据合并为一个并对其运行不同的数据

发布于 2024-09-30 18:17:53 字数 748 浏览 3 评论 0原文

我有一张表:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

私野 2024-10-07 18:17:53
select distinct coach1 as coach from tblName
union 
select coach2 from tblName
order by coach

如果您只想要不同的值,请不要使用 Union All (没有 All 子句的联合可以防止重复)

因为您不断更新您的问题:如果您想要其他字段中的值,您将遇到问题,因为根据您使用的不同,它们可能会有所不同。 (最新?最早?随机?)

select distinct coach1 as coach from tblName
union 
select coach2 from tblName
order by coach

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?)

吻安 2024-10-07 18:17:53

我不记得访问的语法,但类似

select distinct coach from (
select coach1 as coach from tableX 
union all 
select coach2 as coach from tableX) 
where coach <> 0

I cannot remember the syntax for access but something like

select distinct coach from (
select coach1 as coach from tableX 
union all 
select coach2 as coach from tableX) 
where coach <> 0
迷路的信 2024-10-07 18:17:53
create view coaches as
  select coach1 as coach
    from table
   where coach1 <> 0
union all
  select coach2 as coach
    from table
   where coach1 <> 0;

select distinct coach
  from coaches;   
create view coaches as
  select coach1 as coach
    from table
   where coach1 <> 0
union all
  select coach2 as coach
    from table
   where coach1 <> 0;

select distinct coach
  from coaches;   
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文