简体   繁体   English

SQL对多个数据行排序而不合并行

[英]SQL order multiple data rows without combining the rows

For a notification system, I am trying to take multiple rows of completely seperate data, and order them using a unix time stamp. 对于通知系统,我正在尝试获取多行完全独立的数据,并使用unix时间戳对其进行排序。 The data from each row is selected by using a users USERID, and then the rows should be ordered from the time they occured. 通过使用用户USERID选择每一行的数据,然后应从出现的时间开始对行进行排序。

for example, one of the tables being selected would be: 例如,选择的表之一将是:

- USERID   msgid    timestamp
-   3        5       1234567

A final table after selecting all relevant data should look like: 选择所有相关数据后的最终表应如下所示:

- USERID    msgid    postid    playerpostid     friendrequestid     timestamp  
-   3         5                                                      1234567
-   3                  5                                             1234566
-   3        1234                                                    1234565
-   3                               542                              1234564

As you can see, the tables all combine, but the only thing connecting them is the USERID, and ordered by the timestamp. 如您所见,这些表全部组合在一起,但是连接它们的唯一方法是USERID,并按时间戳排序。

Ive tried joins, unfortunately that multiples the results to a ridiculous amount of results. Ive尝试了联接,不幸的是将结果乘以大量的结果。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM