mysql 将 2 列移动到 1 列重新排序非 0 的数据
mysql moving 2 columns to one reordening the data where not 0
这个table可以重新排列成一个table(使用视图)
+----+------+--------+
| id | item1| item2 |
+----+------+--------+
| 1 | A | B |
| 2 | 0 | B |
| 3 | A | 0 |
| 4 | 0 | 0 |
+----+------+--------+
将数据移至此排列:
+----+------+
| id |items |
+----+------+
| 1 | A |
| 2 | B |
| 3 | B |
| 4 | A |
+----+------+
我认为要取消数据透视,同时忽略 0
值。
考虑:
select
row_number() over(order by t.id, t.seq) id,
t.item
from (
select id, 1 seq, item1 item from mytable where item1 <> '0'
union all select id, 2, item2 from mytable where item2 <> '0'
) t
order by t.id, t.seq
这个table可以重新排列成一个table(使用视图)
+----+------+--------+
| id | item1| item2 |
+----+------+--------+
| 1 | A | B |
| 2 | 0 | B |
| 3 | A | 0 |
| 4 | 0 | 0 |
+----+------+--------+
将数据移至此排列:
+----+------+
| id |items |
+----+------+
| 1 | A |
| 2 | B |
| 3 | B |
| 4 | A |
+----+------+
我认为要取消数据透视,同时忽略 0
值。
考虑:
select
row_number() over(order by t.id, t.seq) id,
t.item
from (
select id, 1 seq, item1 item from mytable where item1 <> '0'
union all select id, 2, item2 from mytable where item2 <> '0'
) t
order by t.id, t.seq