使用mysql将两列视为一

时间:2018-07-27 18:30:42

标签: mysql sql

我有一张桌子,上面有两笔付款,每笔都有自己的日期,就像这样:

table
______________
id
.......
payment1
payment1_date
payment2
payment2_date

我需要的是付款的有序列表,而忽略是付款1还是付款2。

付款既可以是空的,也可以是一次,也可以全部填充。

结果如下:

id        payments      date         ... 
3         (payment1)    2018-08-01
5         (payment2)    2018-08-03
4         (payment2)    2018-08-04
3         (payment2)    2018-08-07

付款1将是预付款,其余的将是付款2,但对于会计信息,我只想显示按日期排序的所有付款,即付款日期x_date

1 个答案:

答案 0 :(得分:2)

我会尝试的:

    select id,payment1 as payment, payment1_date as payment_date
    from table
    where payment1 is not null or payment1 <> ''
    union 
    select id,payment2 as payment, payment2_date as payment_date
    from table
    where payment2 is not null or payment2 <> ''
    order by 3