MySQL 在同一个 table 上左连接,双重反转结果

MySQL left join on the same table, double inverted result

我有这个table:

item | timestamp | value
x | 1 | 30
x | 2 | 40
x | 3 | 50
y | 1 | 25
y | 3 | 15

我想得到他的结果:

timestamp | xval | yval
1 | 30 | 25
3 | 50 | 15
2 | 40 | NULL

但是对于这个查询,我得到了双重不需要的结果:

select x.timestamp, x.value as xval, y.value as yval from test x left join test y on x.timestamp=y.timestamp and x.item!=y.item

结果是:

timestamp | xval | yval
1 | 25 | 30
3 | 15 | 50
1 | 30 | 25
3 | 50 | 15
2 | 40 | NULL

我想要一个没有前两行的结果。

这里应该可以使用基本的数据透视表查询:

SELECT
    timestamp,
    MAX(CASE WHEN item = 'x' THEN value ELSE NULL END) AS xval,
    MAX(CASE WHEN item = 'y' THEN value ELSE NULL END) AS yval
FROM test
GROUP BY
    timestamp
ORDER BY
    timestamp;

Demo