MYSQL命令显示唯一值并且后续相同的值为空或空

时间:2018-04-02 01:20:10

标签: mysql sql

我有3个表usersleaveleave_allotment

是否可以查询yearleave_alloted列以显示一次唯一值,而其他相同值的其他值则更改为NULLspace,请参阅表2比较表1?

+----------+------------+------------+-----------------+----------+------+---------------+
| username |  start     |    end     | leave_type      | workdays | year| leave_alloted |
+----------+------------+------------+-----------------+----------+------+---------------+
| admin    | 2017-03-12 | 2017-03-12 | Annual Leave    | 2.0      | 2017 |             7 |
| admin    | 2017-03-12 | 2017-03-12 | Sick Leave      | 2.0      | 2017 |             7 |
| admin    | 2018-03-22 | 2018-03-22 | Leave in Lieu   | 1.0      | 2018 |            14 |
| admin    | 2018-03-23 | 2018-03-23 | Annual Leave    | 0.5      | 2018 |            14 |
| admin    | 2018-03-12 | 2018-03-12 | Unpaid Leave    | 1.0      | 2018 |            14 |
| admin    | 2018-03-23 | 2018-03-23 | Annual Leave    | 1.0      | 2018 |            14 |
| admin    | 2018-03-23 | 2018-03-23 | Annual Leave    | 0.5      | 2018 |            14 |
| admin    | 2018-03-12 | 2018-03-12 | Emergency Leave | 3.0      | 2018 |            14 |
| admin    | 2018-03-28 | 2018-03-28 | Unpaid Leave    | 1.0      | 2018 |            14 |
+----------+------------+------------+-----------------+----------+------+---------------+ 

表2。

+----------+------------+------------+-----------------+----------+------+---------------+
| username |    start   |    end     | leave_type      | workdays | year | leave_alloted |
+----------+------------+------------+-----------------+----------+------+---------------+
| admin    | 2017-03-12 | 2017-03-12 | Annual Leave    | 2.0      | 2017 |             7 |
| admin    | 2017-03-12 | 2017-03-12 | Sick Leave      | 2.0      |      |               |
| admin    | 2018-03-22 | 2018-03-22 | Leave in Lieu   | 1.0      | 2018 |            14 |
| admin    | 2018-03-23 | 2018-03-23 | Annual Leave    | 0.5      |      |               |
| admin    | 2018-03-12 | 2018-03-12 | Unpaid Leave    | 1.0      |      |               |
| admin    | 2018-03-23 | 2018-03-23 | Annual Leave    | 1.0      |      |               |
| admin    | 2018-03-23 | 2018-03-23 | Annual Leave    | 0.5      |      |               |
| admin    | 2018-03-12 | 2018-03-12 | Emergency Leave | 3.0      |      |               |
| admin    | 2018-03-28 | 2018-03-28 | Unpaid Leave    | 1.0      |      |               |
+----------+------------+------------+-----------------+----------+------+---------------+

1 个答案:

答案 0 :(得分:0)

您可以使用变量。在SQL Fiddle

上查看这个简单的示例

MySQL 5.6架构设置

select 
   case when (@previous_a<>a and @previous_b<>b) then c else null end as c,
   @previous_a:=a as a,
   @previous_b:=b as b
from t, (select @previous_a := -1,
                @previous_b := -1) T2
order by a,b

查询1

|      c | a | b |
|--------|---|---|
|      1 | 1 | 1 |
| (null) | 1 | 1 |
| (null) | 1 | 1 |
| (null) | 1 | 1 |
|      2 | 2 | 2 |
| (null) | 2 | 2 |

<强> Results

WebServiceHost WCFService = new WebServiceHost(wcs, httpBaseAddress);
WCFService.Open();
WCFService.Close(); // Is this line of code required when using WebServiceHost 

另外,您可以了解How to create a Minimal, Complete, and Verifiable example