每列的SQL唯一值

时间:2017-05-21 14:42:50

标签: mysql sql

所以我试图从每一列中获取唯一值。到目前为止,我已经尝试过了。

日:周一,周二,周三,周四,周五

时间:上午8-10点,晚上10-12点,下午1-3点,下午3-5点,下午5-7点

SELECT DISTINCT Day, Time FROM table GROUP BY Day, Time;

实施例

Monday    8-10am
Monday    3-5pm
Monday    5-7pm
Tuesday   10-12pm
Tuesday   5-7pm
Wednesday 3-5pm
Thursday  10-12pm
Thursday   1-3pm
Friday    8-10am
Friday    1-3pm

这不会给我每列的唯一值,但是每天的每个TIME的唯一值。

SELECT
(SELECT group_concat(DISTINCT Day) FROM workingTimeDate) as Day,
(SELECT group_concat(DISTINCT Time) FROM workingTimeDate) as Time
FROM workingTimeDate;

这会给:

Monday,Tuesday,Wednesday,Thursday,Friday   8-10am,10-12pm,1-3pm,3-5pm,5-7pm

这会将值组合成两列,但我希望它们位于不同的列中。

我想要的是

Day        Time
Monday     8-10am
Tuesday    10-12pm
Wednesday  1-3pm
Thursday   3-5pm
Friday     5-7pm

2 个答案:

答案 0 :(得分:1)

你可以这样做:

select distinct day, cast(NULL as time) from t
union all
select distinct cast(NULL as date), time from t;

答案 1 :(得分:0)

所以你想要的是:

 select Day, 
        group_concat(time, ', ') as t
   FROM table 
  GROUP BY Day