MySQL如何将两列合并为一列,只显示不同的值?

时间:2012-03-07 16:38:31

标签: mysql select

我有下表

表:时间

timeid   time
1        10    
2        11   
3        3    

表:计数

countId timeID numberCounts times
1       1      1            0
2       2      3            11
2       2      3            12
3       2      3            13
4       3      1            0

查询我正在使用

SELECT

t.time AS "Start Channel",
c.times AS Lapse

FROM Times t

JOIN Count c ON c.Time_ID=t.Time_ID;

输出

+---------------+----------+
| Start Time    | lapse    |
+---------------+----------+
|          10   |        0 | 
|          11   |       11 | 
|          11   |       12 | 
|          11   |       13 | 
|           3   |        0 | 
+---------------+----------+

欲望结果:

+---------------+
|     TimeS     |
+---------------+
|          10   | 
|          11   | 
|          12   | 
|          13   | 
|           3   | 
+---------------+

2 个答案:

答案 0 :(得分:5)

根据您想要的结果,您似乎想要:

SELECT GREATEST(t.time, c.times) AS `TimeS`
  FROM Times AS t
  JOIN Count AS c
    ON c.Time_ID = t.Time_ID
;

但你问题的标题听起来像你想要的那样:

SELECT time AS `TimeS`
  FROM Times
UNION
SELECT times
  FROM Count
;

(在结果中也会包含0,因为Count.times有时是0)。

答案 1 :(得分:0)

您需要学习DISTINCT关键字,简单快捷的解决方案:)