sql—用mysql将两列视为一列

xlpyo6sf  于 2021-06-20  发布在  Mysql
关注(0)|答案(1)|浏览(251)

我有一张table,上面有两个付款设备,每个都有自己的日期,比如:

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

我需要的是一个有序的付款清单,忽略它是payment1还是payment2。
付款可以同时为空、只能为一次或同时为两次。
结果如下:

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

payment1是预付款,payment2是其余部分,但对于会计信息,我只想显示按日期排序的所有付款,即该日期paymentx\ U日期

9njqaruj

9njqaruj1#

我想试试:

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

相关问题