对于通知系统,我试图获取多行完全独立的数据,并使用unix时间戳对它们进行排序。通过使用用户USERID选择每行的数据,然后应该从它们发生的时间开始排序。
例如,正在选择的表之一是:
- USERID msgid timestamp
- 3 5 1234567
选择所有相关数据后的最终表应如下所示:
- USERID msgid postid playerpostid friendrequestid timestamp
- 3 5 1234567
- 3 5 1234566
- 3 1234 1234565
- 3 542 1234564
正如您所看到的,这些表都是组合在一起的,但连接它们的唯一方法是USERID,并按时间戳排序。
我已尝试加入,不幸的是,这会将结果乘以荒谬的结果。
答案 0 :(得分:4)
SELECT * FROM (
SELECT
USERID, msgid, NULL AS postid, NULL AS playerpostid, NULL AS friendrequestid, timestamp
FROM messages
WHERE USERID=3
UNION ALL
SELECT
USERID, NULL AS msgid, postid, NULL AS playerpostid, NULL AS friendrequestid, timestamp
FROM posts
WHERE USERID=3
UNION ALL
SELECT
USERID, NULL AS msgid, NULL AS postid, playerpostid, NULL AS friendrequestid, timestamp
FROM playerposts
WHERE USERID=3
UNION ALL
SELECT
USERID, NULL AS msgid, NULL AS postid, NULL playerpostid, friendrequestid, timestamp
FROM friendrequests
WHERE USERID=3
) AS baseview
ORDER BY timestamp